RxJs Subject.subscribe方法无法按预期工作 [英] RxJs Subject.subscribe method not working as expected
问题描述
Subject.subscribe
方法在被调用时输出以下错误:
Subject.subscribe
method when called outputs the following error:
TypeError: Cannot read property '_subscribe' of undefined
at BidirectionalSubject._subscribe (Rx.js:10239)
at BidirectionalSubject._subscribe (Rx.js:10239)
at BidirectionalSubject.Observable.subscribe (Rx.js:9924)
at AppComponent.doIt (app.component.ts:32)
at ChangeDetector_AppComponent_0.handleEventInternal (eval at ChangeDetectorJITGenerator.generate (angular2.dev.js:1), <anonymous>:29:29)
at ChangeDetector_AppComponent_0.AbstractChangeDetector.handleEvent (angular2.dev.js:8788)
at AppView.dispatchEvent (angular2.dev.js:9396)
at AppView.dispatchRenderEvent (angular2.dev.js:9391)
at DefaultRenderView.dispatchRenderEvent (angular2.dev.js:7819)
at eventDispatcher (angular2.dev.js:9781)
请参见Plunkr 此处.
这是我面临的一个更大问题的一部分. 在我的实际代码中,subscribe
方法不会抛出,但不会在subject
中添加observer
,因此在所有subject.next
调用中,没有人接收到发出的数据.
See Plunkr here.
This is a part of a bigger problem I am facing. In my actual code, the subscribe
method does not throw but it simply does not add an observer
to the subject
, and hence on all subject.next
invocations no one receives the emitted data.
我认为这些问题是相关的.在我的实际代码中,我正在使用rxjs的5.0.0-beta.12
版本.但是在plukr中,rx
依赖关系似乎来自角度本身.
I think the problems are related. In my actual code I am using version 5.0.0-beta.12
of rxjs. In the plukr though, the rx
dependency seems to be coming from angular itself.
推荐答案
请注意使用Subject.create()
.
Be aware of using Subject.create()
.
这与new Subject()
不同,并且绝对多数时候您只想使用new Subject()
而不是Subject.create()
.使用Subject.create()
,您将创建一个AnonymousSubject
实例,该实例从不进行预订,因此flatMap()
运算符在尝试将AnonymousSubject
预订到另一个AnonymousSubject
时抛出错误.
This is not the same as new Subject()
and absolutely most of the time you want to use just new Subject()
instead of Subject.create()
. With Subject.create()
you're creating an instance of AnonymousSubject
which never subscribes itself and therefore the flatMap()
operator throws an error when trying to subscribe AnonymousSubject
to another AnonymousSubject
.
请参阅我对类似问题的回答:使用以下方法创建的主题Subject.create无法退订.
See my answer to a similar question: Subjects created with Subject.create can't unsubscribe.
我刚刚将Subject.create()
更改为new Subject()
,它可能正在工作.
I just changed Subject.create()
to new Subject()
and it's probably working.
查看更新的演示: https://plnkr.co/edit/6M1lPLZA16vwQsVAjNzc?p=预览
尽管,我不知道该演示程序应该做什么,所以我无法判断它是否正常工作.
Although, I don't know what's that demo supposed to do so I can't tell whether it's working correctly.
这篇关于RxJs Subject.subscribe方法无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!