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

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

更新:原来还排除了zone.js和reflect-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作为服务器的根,node_modules实际上与src处于同一级别而不在其中.

任何帮助都将一如既往地受到赞赏.

解决方法:

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

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.js和reflect-metadata也不再包含在Angular 2中,我也遇到了这个问题.

对于那些感兴趣的人,我通过在index.html中包含angular2-polyfills.js文件,最容易解决缺少zone.js和反射元数据的问题:

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

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

上一篇:javascript – 如何使用Angular Http Interceptor和RxJS刷新JWT令牌?


下一篇:javascript – 如何在角度2中取消订阅多个订阅者