在ngrx中获取当前状态 [英] Getting current state in ngrx

查看:107
本文介绍了在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=1ReplaySubject,这意味着仅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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆