更新时间:2023-01-27 17:08:29
Subject
或 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.