且构网

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

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

更新时间:2023-01-27 17:04:00

A 主题 Observable 没有当前值。当一个值被发出时,它被传递给订阅者并且 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.