RxJs Subject.subscribe 方法未按预期工作

RxJs Subject.subscribe method not working as expected

调用

Subject.subscribe 方法时输出以下错误:

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 here


这是我面临的更大问题的一部分。 在我的实际代码中,subscribe 方法不会抛出,但它不会将 observer 添加到 subject,因此在所有 subject.next 调用中没有人收到发出的数据。

我认为这些问题是相关的。在我的实际代码中,我使用的是 5.0.0-beta.12 版本的 rxjs。不过在 plukr 中,rx 依赖项似乎来自 angular 本身。

注意使用 Subject.create().

这与 new Subject() 不同,绝对大多数时候您只想使用 new Subject() 而不是 Subject.create()。使用 Subject.create() 您正在创建一个 AnonymousSubject 的实例,它从不订阅自己,因此 flatMap() 运算符在尝试将 AnonymousSubject 订阅到另一个 AnonymousSubject 时抛出错误.

请参阅我对类似问题的回答:

我刚刚将 Subject.create() 更改为 new Subject(),它可能有效。

查看更新后的演示:https://plnkr.co/edit/6M1lPLZA16vwQsVAjNzc?p=preview

不过,我不知道该演示应该做什么,所以我无法判断它是否正常工作。