且构网

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

如何获取 RxJS Subject 或 Observable 的当前值?

更新时间:2023-01-27 17:08:29

SubjectObservable 没有当前值.当一个值被发出时,它被传递给订阅者并且 Observable 用它完成.

A Subject or Observable doesn't have a current value. When a value is emitted, it is passed to subscribers and the Observable is done with it.

如果您想获得当前值,请使用专为此目的而设计的 BehaviorSubject.BehaviorSubject 保留最后发出的值并立即将其发送给新订阅者.

If you want to have a current value, use BehaviorSubject which is designed for exactly that purpose. BehaviorSubject keeps the last emitted value and emits it immediately to new subscribers.

它还有一个方法 getValue() 来获取当前值.

It also has a method getValue() to get the current value.