如何在ngrx中注射初始状态

时间:2017-08-11 03:49:15

标签: angular ngrx ngrx-store

我正在Angular 4应用中实施ngrx州管理。直到我尝试使用以前保存在浏览器本地存储中的状态“保湿”应用程序状态时,情况一直很顺利。

我对ngrx/store initialStateFromSomewhere文档有疑问。具体来说,以下行是什么意思,我将如何设置(“在运行时动态注入”)/// Pretend this is dynamically injected at runtime const initialStateFromSomewhere = { counter: 3 }; 到从浏览器本地存储中检索的状态?

didSelectRowAt

1 个答案:

答案 0 :(得分:1)

创建减速器时,您可以为商店提供初始状态。

假设您有FeatureState

interface FeatureState {
  counter: number;
}

现在在你的reducer中你需要创建initialState

const initialState: FeatureState = {
  count : 0;
}

此初始状态将提供给reducer

中的州
export function reducer(state: FeatureState = initialState, action: Action): State {

现在,如果您想动态添加initialState,可以从商店中检索initialState并将其传递到reducer

相关问题