【问题标题】:SystemJS loads many files for rxjsSystemJS 为 rxjs 加载许多文件
【发布时间】:2016-11-24 07:30:59
【问题描述】:

我有以下systemjs.config.js(基于我在互联网上找到的一些示例):

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'js:': 'js/',
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'app',
            // angular bundles
            '@angular/core': 'js:angular2/core.umd.js',
            '@angular/common': 'js:angular2/common.umd.js',
            '@angular/compiler': 'js:angular2/compiler.umd.js',
            '@angular/platform-browser': 'js:angular2/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'js:angular2/platform-browser-dynamic.umd.js',
            '@angular/http': 'js:angular2/http.umd.js',
            '@angular/router': 'js:angular2/router.umd.js',
            '@angular/forms': 'js:angular2/forms.umd.js',
            '@angular/upgrade': 'js:angular2/upgrade.umd.js',
            // other libraries
            'rxjs': 'js:rxjs',
            'angular-in-memory-web-api': 'js:in-memory-web-api.umd.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            },
            rxjs: {
                defaultExtension: 'js'
            }
        }
    });
})(this);

当我启动 Angular2 应用程序时,会加载许多单独的 rxjs 文件,这需要很长时间。我在js/rxjs/bundles/Rx.js 中有一个捆绑版本的 RxJs,所以我尝试像这样修改systemjs.config.js

(function (global) {
    System.config({
        paths: {
            // paths serve as alias
            'js:': 'js/',
        },
        // map tells the System loader where to look for things
        map: {
            // our app is within the app folder
            app: 'app',
            // angular bundles
            '@angular/core': 'js:angular2/core.umd.js',
            '@angular/common': 'js:angular2/common.umd.js',
            '@angular/compiler': 'js:angular2/compiler.umd.js',
            '@angular/platform-browser': 'js:angular2/platform-browser.umd.js',
            '@angular/platform-browser-dynamic': 'js:angular2/platform-browser-dynamic.umd.js',
            '@angular/http': 'js:angular2/http.umd.js',
            '@angular/router': 'js:angular2/router.umd.js',
            '@angular/forms': 'js:angular2/forms.umd.js',
            '@angular/upgrade': 'js:angular2/upgrade.umd.js',
            // other libraries
            'rxjs': 'js:rxjs/bundles/Rx.js',
            'angular-in-memory-web-api': 'js:in-memory-web-api.umd.js'
        },
        // packages tells the System loader how to load when no filename and/or no extension
        packages: {
            app: {
                main: './main.js',
                defaultExtension: 'js'
            }
        }
    });
})(this);

当我现在尝试启动我的应用程序时,我在浏览器中收到以下错误:

Error: (SystemJS) Syntax error
    SyntaxError: Syntax error
       at Anonymous function (eval code:2:1)
    Evaluating http://localhost:37191/js/rxjs/bundles/Rx.js/Subject
    Evaluating http://localhost:37191/app/main.js
    Error loading http://localhost:37191/app/main.js

我的main.ts 看起来像这样:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app.module';
const platform = platformBrowserDynamic();
platform.bootstrapModule(AppModule);

而我的app.module.ts 看起来像这样:

import 'rxjs';

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';

import { AppRoutingModule } from './app-routing.module';

import { AppComponent } from './app.component';
import { DeviceGroupsViewComponent } from './device-groups-view.component';

import { DeviceGroupService } from './devicegroup.service';
import { SourceTypeService } from './sourcetype.service';

@NgModule({
    imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        AppRoutingModule
    ],
    declarations: [
        AppComponent,
        DeviceGroupsViewComponent
    ],
    providers: [
        DeviceGroupService,
        SourceTypeService
    ],
    bootstrap: [AppComponent]
})
export class AppModule { }

要使我的应用与捆绑的 Rx.js 一起工作,我需要进行哪些更改?

【问题讨论】:

  • 您找到最佳解决方案了吗?

标签: angular typescript rxjs systemjs rxjs5


【解决方案1】:

如果您的设置基于 angular2 quickstart,那么您就可以了。

但是,你绝不能

import {something} from 'rxjs/Rx';

这是错误的,会导入整个 RxJS 库(当你只想使用 Observable 时)。

import {Observable} from 'rxjs/Rx';

这是正确的

import {Observable} from 'rxjs/Observable';

并且会减少导入的数量。确保您的应用没有引用“rxjs/Rx”

PS 你不想把整个 RxJS 库放在一个文件中,因为它会非常大。这就是只导入你需要的 RxJS 模块的全部原因。

【讨论】:

  • 进行上述更改确实减少了正在加载的 rxjs 库的数量。但是为什么会出现这种行为,为什么在任何地方都没有提到呢?
【解决方案2】:

前段时间我做了这个演示,它使用 Angular2 并将 RxJS 作为单个包加载。

基本上,我所做的只是:

paths: {
    'rxjs*': 'https://unpkg.com/@reactivex/rxjs@5.0.0-beta.12/dist/global/Rx.js'
},

其中唯一重要的是 rxjs* 匹配例如 rxjs/Subjectrxjs/add/operator/concat 等等。

在 plnkr 上查看现场演示:http://plnkr.co/edit/rnO74mQNuPsHAmrIVJ8j?p=preview

【讨论】:

  • 下一个主要 SystemJS 版本 - 0.20 中路径 will not be supported 中的通配符,因此看起来加载 Rx.js 包的唯一方法是通过脚本标记,就像在早期的 angular2 RC 中所做的那样例子。
  • @artem 这很有趣,我不知道!看起来它会以同样的方式工作,只是没有*,正如评论中看到的github.com/systemjs/systemjs/issues/1039#issuecomment-251283268
  • 根据我的经验,没有通配符,只有当路径条目的键和值都以 / 结尾时,它才能工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多