缺少可观察的方法RxJS 5.0.0-beta.0 [英] Missing observable methods RxJS 5.0.0-beta.0
问题描述
我在使用带有Angular 2的RxJS时遇到了问题。在我的Observable对象中没有定义大多数从Typescript定义文件建议的方法,如...
I'm having a problem using RxJS with Angular 2. Most of methods suggested from Typescript definition file are not defined on my Observable object like...
< a href =https://i.stack.imgur.com/6qhS4.png =noreferrer>
然后我发现,Observable原型上不存在方法。
then I figured out, that methods does not exists on the Observable prototype.
我知道很多事情从版本4改为5,所以我会错过什么?
Browserify添加了它对我来说...
Browserify added it for me...
推荐答案
如果没有看到您的实际代码,我无法确切地告诉您要添加什么来修复它。
Without seeing your actual code, I can't tell you exactly what to add to fix it.
但一般问题是:RxJS 5不是现在Angular 2已经进入Beta阶段了。您需要导入所需的运算符,或者全部导入它们。 import语句如下所示:
But the general problem is this: RxJS 5 is not included with Angular 2 any longer now that it has entered the Beta stage. You will need to import either the operator(s) you want, or import them all. The import statements looks like this:
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
要确定所需模块的路径,请查看源代码树。每次导入 add
都会将属性添加到 Observable
或 Observable.prototype
。如果没有添加
,您需要从'rxjs / path /到/ foo'执行 import {foo}。
To determine the path to your desired module, look at the source tree. Every import with add
will add properties to Observable
or Observable.prototype
. Without add
, you'd need to do import {foo} from 'rxjs/path/to/foo'
.
您还需要确保正确地将RxJS带入项目中。这样的东西会进入你的index.html文件:
You will also need to make sure that RxJS is being brought into the project correctly. Something like this would go into your index.html file:
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在这个Github项目中构建Angular 2应用程序(就像我一样),那么你就没有需要系统
的东西和导入应该这样做。
If you use Webpack to build the Angular 2 app like in this Github project (like I did), then you don't need that System
stuff and the imports should do it.
这篇关于缺少可观察的方法RxJS 5.0.0-beta.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!