首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自unpkg的rxjs包

来自unpkg的rxjs包
EN

Stack Overflow用户
提问于 2016-09-29 16:31:00
回答 2查看 1.7K关注 0票数 3

我有一个角2.0.0 (在2.2.0中同样的问题)项目。当前的dev构建会生成>100个http请求。

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

当我有以下情况时:

代码语言:javascript
复制
    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加载

代码语言:javascript
复制
    paths: {
        // paths serve as alias
        'npm:': 'https://unpkg.com/',
        'rxjs/*': 'https://unpkg.com/@reactivex/rxjs/dist/global/Rx.js'
    },

ReactiveX git页面建议这个包应该工作,还是缺少其他东西??

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

代码语言:javascript
复制
Uncaught (in promise): TypeError: Cannot read property 'call' of undefined
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-12-02 09:00:43

详细讨论了这个问题,这里和解决方案是使用您自己的加载程序,例如(代码信用nros )。

显然,它将在未来的版本中得到修正--我怀疑这是由于rxjs还处于beta版而被推迟的。

rxjsLoader.js

代码语言:javascript
复制
// 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, "$1").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

代码语言:javascript
复制
SystemJS.config({

    baseURL: '/',

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

Stack Overflow用户

发布于 2016-09-30 09:47:39

我有一个plnkr.io演示,用于在TypeScript中使用RxJS来加载捆绑版本,就像您一样。

请参阅:http://plnkr.co/edit/0o9j6h5GJJiCuBFeWv7f

重要的部分是System.config

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

然后从包中导入任何内容,只需:

代码语言:javascript
复制
import {BehaviorSubject} from 'rxjs';

编辑:

更新了默认的Angular2演示程序,以加载单个Rx.js

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

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

代码语言:javascript
复制
'rxjs*': 'https://unpkg.com/@reactivex/rxjs@5.0.0-beta.12/dist/global/Rx.js'

然后我使用它,例如:

代码语言:javascript
复制
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);
  }

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39775577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档