且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

RxJS 订阅未触发

更新时间:2023-10-29 23:04:52

"Subject" observable 不会在订阅时重放之前的值.只有在您的服务中[以某种方式] 执行以下操作时,您的订阅才会触发 -

"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(<your new number>);

要在每次订阅时触发订阅[获取最后发出的值],您应该使用BehaviorSubject"而不是像 -

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

private selectedCourseIndexUpdated = new BehaviorSubject<number>(0);

BehaviorSubject 在每个订阅上重放最后发出的值.注意 BehaviorSubject 构造函数采用默认值.

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