我在将 RxJS 与 Angular 2 一起使用时遇到问题。Typescript 定义文件中建议的大多数方法都没有在我的 Observable 对象上定义,例如...
然后我发现,Observable 原型(prototype)上不存在该方法。
我知道从版本 4 到版本 5 有很多变化,所以我错过了什么吗?
最佳答案
没有看到您的实际代码,我无法确切地告诉您要添加什么来修复它。
但普遍的问题是:RxJS 5 不再包含在 Angular 2 中,因为它已经进入 Beta 阶段。您需要导入所需的运算符,或全部导入。导入语句如下所示:
import 'rxjs/add/operator/map'; // imports just map
import 'rxjs/add/operator/mergeMap'; // just mergeMap
import 'rxjs/add/operator/switchMap'; // just switchMap
import {delay} from 'rxjs/operator/delay'; // just delay
或者喜欢
import 'rxjs/Rx'; // import everything
要确定所需模块的路径,请查看 source tree .每次使用 add
导入都会将属性添加到 Observable
或 Observable.prototype
。如果没有 add
,您需要执行 import {foo} from 'rxjs/path/to/foo'
。
您还需要确保将 RxJS 正确引入项目。像这样的东西会进入你的 index.html 文件:
System.config({
map: {
'rxjs': 'node_modules/rxjs' // this tells the app where to find the above import statement code
},
packages: {
'app': {defaultExtension: 'js'}, // if your app in the `app` folder
'rxjs': {defaultExtension: 'js'}
}
});
System.import('app/app'); // main file is `app/app.ts`
如果您使用 Webpack to build the Angular 2 app like in this Github project (就像我做的那样),那么你不需要那个 System
的东西,导入应该做。
关于http - 缺少可观察方法 RxJS 5.0.0-beta.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34548924/