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

查看:18
本文介绍了在 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(最新的) 值被缓存并在订阅时重播 - 所以 take(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天全站免登陆