如何使用 systemjs 在最小的 Angular 2 应用程序中加载 RxJS?

How to load RxJS in a minimal Angular 2 app using systemjs?

我无法获得使用 RxJS 的最小 Angular 2 应用程序。我正在使用 Typescript (tsc 1.6.2) 和 systemjs 进行模块加载。如何让 systemjs 正确加载 Rx 模块?我 运行 没有想法可以尝试,如果能指出我做错了什么,我将不胜感激。模块加载对我来说有点神奇。很郁闷。

index.html:

<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <script src="../node_modules/es6-shim/es6-shim.js"></script>
    <script src="../node_modules/systemjs/dist/system.src.js"></script>
    <script src="../node_modules/rx/dist/rx.lite.js"></script>
    <script src="../node_modules/angular2/bundles/angular2.dev.js"></script>
</head>

<body>
    <app>App loading...</app>
    <script>
        System.config({
            packages: { 'app': { defaultExtension: 'js' } }
        });
        System.import('app/app');
    </script>
</body>
</html>

app.ts:

/// <reference path="../../node_modules/rx/ts/rx.all.d.ts" />
import { bootstrap, Component, View } from 'angular2/angular2';
import * as Rx from 'rx';

@Component({
    selector: 'app'
})
@View({
        template: `Rx Test`
})
export class App {
    subject: Rx.Subject<any> = new Rx.Subject<any>();
}
bootstrap(App);

SystemJS 尝试加载不存在的文件:

一旦我在上面的代码中注释掉 subject,一切 运行 都很好,因为不会尝试加载不存在的文件(并且没有加载 rx)。

更新angular2 beta 0

Angular2 不再在 Angular2 中捆绑 RxJS。现在您必须单独导入运算符。这是一个重要的突破性变化,我 。因此,请参考该答案,因为该答案已过时且不再适用。

2015 年 12 月 11 日更新

Alpha46 使用 RxJS alpha 0.0.7(即将成为 0.0.8)。由于这个 ng2 alpha 版本你不再需要下面的解决方案,你现在可以直接从 angular2/angular 导入 ObservableSubject 等,因此可以丢弃原始答案

import {Observable, Subject} from 'angular2/angular2';

=====================================

Angular2 不再使用旧的 RxJS, they moved to the new RxJS 5(又名 RxJS Next),因此它将与 Http 和 EventEmitter 发生冲突。

所以首先删除对 rx.lite.js 的导入和脚本。

相反,您必须这样做(您的配置中不需要脚本或映射)

编辑

这一行是为了让它在 plnkr 中工作,但在我的项目中我只需要添加其他东西

Plnkr 版本

import Subject from '@reactivex/rxjs/dist/cjs/Subject';

离线版

import * as Subject from '@reactivex/rxjs/dist/cjs/Subject';

离线版注意事项

如果您在本地项目中尝试 plnkr 的第一种方法,您可能会收到此消息错误

TypeError: Subject_1.default is not a function

因此对于您的本地(离线)版本,您应该使用第二种方法(带有星号)。

Subject 中没有括号,this conversation 中对此进行了解释(我遇到了同样的问题,哈哈)

这是 plnkr not failing.

希望对您有所帮助。如果我错过了什么,请告诉我 ;)

对于 angular2 alpha52 使用 rxjs 5alpha.14

<script>
System.config({
  map: { rxjs: 'node_modules/rxjs' },
  packages: {
    rxjs: { defaultExtension: 'js' },
    'app': {defaultExtension: 'js'}
  }
});
System.import('app/app');
</script>

但是你必须像这个例子一样单独导入每个运算符

import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';

require('rxjs/add/operator/map');
require('rxjs/add/operator/scan');
require('rxjs/add/operator/mergemap');  //for flatmap
require('rxjs/add/operator/share');
require('rxjs/add/operator/combinelatest-static'); //for combinelatest

在 Angular 4 发布后发布此答案。尝试从 Rxjs 加载最低限度,因为 Angular prod 构建即使使用 AOT 也是 300kb

希望对您有所帮助。

import { Injectable } from '@angular/core';
import {Http} from "@angular/http";
import {Observable} from "rxjs/Observable";
import 'rxjs/add/operator/map';// load this in main.ts

import {AllUserData} from "../../../shared/to/all-user-data";

@Injectable()
export class ThreadsService {

  constructor(private _http: Http) { }

  loadAllUserData(): Observable<AllUserData> {
    return this._http.get('/api/threads')
      .map(res => res.json());
  }

}