问题描述
我在使用带有 Angular 2 的 RxJS 时遇到问题.Typescript 定义文件中建议的大多数方法都没有在我的 Observable 对象上定义,例如...
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...
然后我发现,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 阶段.您将需要导入所需的运算符,或将它们全部导入.导入语句如下所示:
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
添加属性.如果没有 add
,你需要从 'rxjs/path/to/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 构建 Angular 2 应用程序,就像在这个 Github 项目中一样(就像我做的那样),那么你就不需要 System
的东西,导入就应该这样做.
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屋!
更多推荐
[db:关键词]
发布评论