问题
I have an angular 2.0.0 (same issue in 2.2.0) project. The current dev build generates > 100 http requests.
This is due to it loading non-bundled versions of rxjs..
When I have the following :
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',
},
The app works but we have 1000+ http requsts , so I try to load rxjs from the bundle, todo this I remove rxjs': 'npm:rxjs'
and I add the following
Note, for both the npm: loads from unpkg
paths: {
// paths serve as alias
'npm:': 'https://unpkg.com/',
'rxjs/*': 'https://unpkg.com/@reactivex/rxjs/dist/global/Rx.js'
},
The ReactiveX git page suggests that this package should work or is it missing something else??
I get the following error with the second config
Uncaught (in promise): TypeError: Cannot read property 'call' of undefined
回答1:
I have a plnkr.io demo for fiddling with RxJS in TypeScript that loads the bundled verison just like you.
See: http://plnkr.co/edit/0o9j6h5GJJiCuBFeWv7f
The important part is System.config
:
System.config({
transpiler: 'typescript',
map: {
'rxjs': 'https://unpkg.com/@reactivex/rxjs@5.0.0-beta.12/dist/global/Rx.js'
}
});
Then importing anything from the bundle with just:
import {BehaviorSubject} from 'rxjs';
Edit:
The default Angular2 demo updated to load a single Rx.js
.
See it live: http://plnkr.co/edit/rnO74mQNuPsHAmrIVJ8j
In systemjs.config.js
I removed the rxjs
form map
completely and added to paths
:
'rxjs*': 'https://unpkg.com/@reactivex/rxjs@5.0.0-beta.12/dist/global/Rx.js'
Then I'm using it with for example:
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);
}
}
回答2:
This issue is discussed in detail here and the solution is to use your own loader such as (code credit nros
Apparently, it's going to be fixed in a future release I suspect this is being held back as rxjs is still in beta.
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, "$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
SystemJS.config({
baseURL: '/',
map: {
"rxjs": "Rx.js"
},
paths: {
"Rx.js/*": "rxjs-parts/*"
},
packages: {
"rxjs-parts": {
meta: {
"*": {
loader: "rxjsLoader.js"
}
}
}
}
});
来源:https://stackoverflow.com/questions/39775577/rxjs-bundle-from-unpkg