Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.7k views
in Technique[技术] by (71.8m points)

angular - RxJs Subject.subscribe method not working as expected

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)

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Be aware of using Subject.create().

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.

See my answer to a similar question: Subjects created with Subject.create can't unsubscribe.

I just changed Subject.create() to new Subject() and it's probably working.

See your updated demo: https://plnkr.co/edit/6M1lPLZA16vwQsVAjNzc?p=preview

Although, I don't know what's that demo supposed to do so I can't tell whether it's working correctly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...