来自 unpkg 的 rxjs 包

rxjs bundle from unpkg

我有一个 angular 2.0.0(2.2.0 中的相同问题)项目。当前开发版本生成 > 100 个 http 请求。

这是因为它加载了非捆绑版本的 rxjs..

当我有以下情况时:

    map: {
        // our app is within the app folder
        app: 'app',
        // angular bundles
        // snip
        'rxjs': 'npm:rxjs',
        'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
    },

该应用程序可以运行,但我们有 1000 多个 http 请求,所以我尝试从包中加载 rxjs,为此我删除了 rxjs': 'npm:rxjs' 并添加了以下内容

注意,对于 npm:从 unpkg 加载

    paths: {
        // paths serve as alias
        'npm:': 'https://unpkg.com/',
        'rxjs/*': 'https://unpkg.com/@reactivex/rxjs/dist/global/Rx.js'
    },

ReactiveX git 页面表明这个包应该可以工作,还是它缺少其他东西??

我在第二个配置中收到以下错误

Uncaught (in promise): TypeError: Cannot read property 'call' of undefined

我有一个 plnkr.io 在 TypeScript 中摆弄 RxJS 的演示,可以像您一样加载捆绑的版本。

参见:http://plnkr.co/edit/0o9j6h5GJJiCuBFeWv7f

重要的部分是System.config:

System.config({
  transpiler: 'typescript',
  map: {
    'rxjs': 'https://unpkg.com/@reactivex/rxjs@5.0.0-beta.12/dist/global/Rx.js'
  }
});

然后从捆绑包中导入任何东西:

import {BehaviorSubject} from 'rxjs';

编辑:

默认的 Angular2 演示更新为加载单个 Rx.js

现场观看:http://plnkr.co/edit/rnO74mQNuPsHAmrIVJ8j

systemjs.config.js 中,我完全删除了 rxjs 表单 map 并添加到 paths:

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

然后我用它来举例:

import { Component } from '@angular/core';
import { BehaviorSubject } from 'rxjs';

@Component({
  selector: 'my-app',
  template: '<h1>My First Angular App</h1>'
})
export class AppComponent {

  constructor() {
    console.log(BehaviorSubject);
  }

}

详细讨论了这个问题here and the solution is to use your own loader such as (code credit nros

显然,它会在未来的版本中得到修复我怀疑这是因为 rxjs 仍处于测试阶段而被阻止。

rxjsLoader.js

// see: https://github.com/angular/angular/issues/9359
// in case all parts of RxJS are loaded with a single file (eg: Rx.js), Angular 2 may have
// difficulties using/requiring the various parts.
// this custom loader translates requests to these parts to the already loaded Rx entity.
//
// eg: Angular:
//      require('rxjs/observable/from')  -->  Rx.Observable
//      require('rxjs/operator/concatMap')  -->  Rx.Observable.prototype
//      require('rxjs/util/EmptyError')  -->  Rx
//
// Angular will access 'rxjs/observable/from' as rxjs_observable_from.from
// so, the last part of the included module (eg: 'from') denotes the property name to access
// the required value.
SystemJS.amdDefine(SystemJS.baseURL + "rxjsLoader.js", ["rxjs"], function (Rx) {
    'use strict';

    // custom loader for RX.js to instantiate the correct value
    // see: https://github.com/ModuleLoader/es-module-loader/blob/v0.17.0/docs/loader-extensions.md
    return {
        fetch: function fetch(loadData) {
            return ""; // no fetch - "Rx" is already loaded!
        },

        translate: function translate(loadData) {
            return "";
        },

        instantiate: function instantiate(loadData) {

            // loadData.name contains the full URL
            var propertyName = loadData.name.replace(/^.*\/rxjs-parts\/(.*)$/i, "").replace(/\.js$/i, "");

            // if property name is not empty, evaluate and use it
            if (propertyName.length > 0 && !(/^\s*$/.test(propertyName))) {
                var parts = propertyName.split("/"),
                    targetObject = Rx
                ;

                // Angular 2 expects the return value to be an object
                // and the last part of the name to be the property of that object

                for (var i=0; i < parts.length-1; i++) {
                     var partName = parts[i],
                         upperCaseName = partName.charAt(0).toUpperCase() + partName.slice(1)
                     ;

                     // handle special case for "operator/*"
                     if (partName === "operator") {
                         return Rx.Observable.prototype;

                     } else if (targetObject[partName] !== undefined) {
                         targetObject = targetObject[partName];

                     } else if (targetObject[upperCaseName] !== undefined) {
                         targetObject = targetObject[upperCaseName];

                     } else {
                         // skip name and try with next part name. eg: "utils"
                         continue;
                     }
                }

                return targetObject;

            } else {
                // return the Rx as default
                return Rx;
            }
        }
    };
});

systemjs-config-using-custom-rx-loader.js

SystemJS.config({

    baseURL: '/',

    map: {
        "rxjs": "Rx.js"
    },
    paths: {
        "Rx.js/*": "rxjs-parts/*"
    },
    packages: {
       "rxjs-parts": {
            meta: {
                "*": {
                    loader: "rxjsLoader.js"
                }
            }
        }
    }
});