问题描述
我使用AngularJS 2 Beta版0,我试图创建一个从窗口对象上的一个事件观测的RxJS。我相信我知道捕获事件作为一个可观察在我服务的公式:
I'm using AngularJS 2 Beta 0 and I'm trying to create an RxJS Observable from an event on a window object. I believe I know the formula for capturing the event as an Observable in my service:
var observ = Observable.fromEvent(this.windowHandle, 'hashchange');
问题是,每次我试图运行此code的时候,我得到一个错误,指出fromEvent'是不是一个函数。
The problem is that every time I try run this code, I get an error stating that 'fromEvent' is not a function.
Uncaught EXCEPTION: Error during evaluation of "click"
ORIGINAL EXCEPTION: TypeError: Observable_1.Observable.fromEvent is not a function
这似乎在暗示,我认为我没有处理我的导入
正确的,现在不包括RxJS在角2的生成,虽然我的应用程序的其它部分功能正常,这对我意味着RxJS是它应该是。
This seems to imply to me that I'm not handling my import
correctly now that RxJS is not included in the build of Angular 2, though the rest of my application functions properly, which to me means that RxJS is where it is supposed to be.
我在服务导入是这样的:
My import in the service looks like this:
import {Observable} from 'rxjs/Observable';
虽然我还试图用这个代替(用适当修改code),具有相同的结果:
Though I have also tried to use this instead (with the appropriate changes to the code), with the same results:
import {FromEventObservable} from 'rxjs/observable/fromEvent';
我有下面的配置,我的index.html:
I have the following configuration in my Index.html:
<script>
System.config({
map: {
rxjs: 'node_modules/rxjs'
},
packages: {
'app': {defaultExtension: 'js'},
'rxjs': {defaultExtension: 'js'}
}
});
System.import('app/app');
</script>
有人能告诉我我在做什么错误?
Can somebody tell me what I'm doing incorrectly?
推荐答案
这个问题似乎是import语句应该是这样的:
The problem seemed to be that the import statement should look like this:
import {Observable} from 'rxjs/Rx';
注意观测
正在从 rxjs /接收
从 rxjs带来的,而不是在/可观察
。 As @EricMartinez提到,这样拉会自动地得到你所有的运营商(如 .MAP()
)。
Note that Observable
is being brought in from rxjs/Rx
instead of from rxjs/Observable
. As @EricMartinez mentions, pulling it in this way will automagically get you all of the operators (like .map()
).
这篇关于Angular2 RxJS获得“Observable_1.Observable.fromEvent不是一个函数”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!