如何使用Angular 2(beta和更新版本)加载RxJS(和zone.js / reflect-metadata)?

时间:2015-12-16 17:54:12

标签: javascript angular rxjs systemjs

从Angular 2 Alpha 54(changelog)开始,RxJS不再包含在Angular 2中。

更新:原来还排除了zone.jsreflect-metadata

因此,我现在收到以下错误(如Chrome开发者控制台中所示):

system.src.js:4681 GET http://localhost:3000/rxjs/Subject 404 (Not Found)F @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681
system.src.js:4681 GET http://localhost:3000/rxjs/observable/fromPromise 404 (Not Found)F @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681
localhost/:1 Uncaught (in promise) Error: XHR error (404 Not Found) loading http://localhost:3000/rxjs/Subject(…)t @ system.src.js:4681g @ system.src.js:4681(anonymous function) @ system.src.js:4681
system.src.js:4681 GET http://localhost:3000/rxjs/operator/toPromise 404 (Not Found)F @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681(anonymous function) @ system.src.js:4681
system.src.js:4681 GET http://localhost:3000/rxjs/Observable 404 (Not Found)

我的package.json文件(^ 5.0.0-beta.0)中有RxJS,并且已经安装了npm,但问题是我对此不熟悉SystemJS此时要让事情顺利进行。这是我的index.html文件的正文部分:

<body>
<app></app> <!-- my main Angular2 tag, which is defined in app/app as referenced below -->

<script src="../node_modules/systemjs/dist/system.js"></script>
<script src="../node_modules/typescript/lib/typescript.js"></script>
<script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
<script src="../node_modules/angular2/bundles/http.dev.js"></script>
<script>
    System.config({
        packages: {'app': {defaultExtension: 'js'}}
    });
    System.import('./app/app');
</script>

<script src="http://localhost:35729/livereload.js"></script>
</body>

我一直在玩其他配置,但没有一个让我一路走来。我的猜测是,这是相对简单的,只是我缺乏理解或工具的使用方式阻止了我。

这是我的app.ts文件,它是SystemJS配置中引用的app/app

import {Component} from 'angular2/core';
import {bootstrap} from 'angular2/platform/browser';
import {COMMON_DIRECTIVES} from 'angular2/common';

@Component({
    selector: 'app',
    directives: [],
    template: `<div>{{greeting}}, world!</div>`
})
export class App {
    greeting:string = 'Hello';
}

bootstrap(App, [COMMON_DIRECTIVES]);

我正在使用使用静态映射的livereload Express服务器为应用程序提供服务,以便http://localhost:3000/node_modules/rxjs/Rx.js之类的调用能够获取所需的文件,即使index.html是从{/src提供的1}}作为服务器的根目录,node_modules实际上与src处于同一级别而不在其中。

任何帮助都会一如既往地受到赞赏。

1 个答案:

答案 0 :(得分:14)

事实证明,通过将SystemJS配置更改为index.html文件中的以下内容,可以轻松解决问题:

System.config({
    map: {
        rxjs: 'node_modules/rxjs' // added this map section
    },
    packages: {
        'app': {defaultExtension: 'js'},
        'rxjs': {defaultExtension: 'js'} // and added this to packages
    }
});
System.import('app/app');

我实际上已经尝试过,但我没有意识到zone.jsreflect-metadata也不再包含在Angular 2中,我也遇到了这个问题。

对于那些感兴趣的人,我最容易通过在我的index.html中包含zone.js文件来解决reflect-metadataangular2-polyfills.js的问题:

<script src="../node_modules/angular2/bundles/angular2-polyfills.js"></script>

现在我的应用程序就像使用Alpha 53一样。