RxJS subscription not firing

前端 未结 2 505
你的背包
你的背包 2021-01-13 17:16

Currently learning RxJS. I have an integer selectedCourseIndex within a service that I\'d like a separate component to subscribe to.

courses-sec

2条回答
  •  情深已故
    2021-01-13 18:00

    "Subject" observable does not replay the previous values on subscribe. Your subscription will only fire if you do the following in your service [somehow] -

    this.selectedCourseIndexUpdated.next();
    

    To fire subscription on each subscribe [to get the last emitted value] you should use "BehaviorSubject" instead of Subject like -

    private selectedCourseIndexUpdated = new BehaviorSubject(0);
    

    BehaviorSubject replay the last emitted value on each subscription. Notice BehaviorSubject constructor takes the default value.

提交回复
热议问题