在ngrx中获取当前状态 [英] Getting current state in ngrx
问题描述
此处是在ngrx中获取当前状态的解决方案.这个例子很简单-您只需使用take(1)
.但是在take
的rxjs文档中它说:
Here is the solution for getting the current state in ngrx. The example is simple - you just use take(1)
. But in rxjs documentation for take
it says:
从的开始处返回指定数量的连续元素. 可观察的序列
Returns a specified number of contiguous elements from the start of an observable sequence
取第一个值如何获取当前状态(即最后一个值)?
How come taking the first value gets the current state (i.e. the last value)?
此外,在使用Subject
进行单元测试时,我还无法模拟这种行为.
Also I'm having trouble mocking this behavior in unit tests using Subject
.
推荐答案
ngrx-store是length=1
的ReplaySubject
,这意味着仅1(最新)值被缓存并在订阅时重播-因此
The ngrx-store is a ReplaySubject
of length=1
, this means only 1(the latest) value is cached and replayed on subscribe - so take(1)
will resolve to the latest value.
这篇关于在ngrx中获取当前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!