ngRx :来自状态接口的键和来自 StoreModule.forRoot 的键之间的差异



我刚刚开始ngRx,我遇到了一个小问题。

我想知道接口状态和模块forRoot键之间的区别。

export interface ConfigurationState {
readonly configuration: Configuration;
}
StoreModule.forRoot({
configuration: ConfigurationReducer,
}),
export function configurationReducer(state: Configuration = undefined, action: ConfigurationActions.Actions): Configuration {
switch (action.type) {
case ConfigurationActions.SET:
return action.payload;
default:
return state;
}
}
this.store.select('configuration')

当我选择包含第 4 段代码的商店时,我看到第 1 段代码键的智能帮助。

如果我更改第二段代码的键,它不会改变任何东西。

所以我的问题是,forRoot的目的是什么?钥匙重要吗?

inerface的工作是打字和一点智能。forRoot是注册您的化简器,这是最重要的部分,否则您的化简器不会被调用。

在您发布的示例中,State接口并没有真正添加任何值,但是如果您执行以下操作,它会添加一些类型检查值。

// reducer.ts
/**
* As mentioned, we treat each reducer like a table in a database. This means
* our top level state interface is just a map of keys to inner state types.
*/
export interface State {
layout: fromLayout.State;
router: fromRouter.RouterReducerState;
}
/**
* Our state is composed of a map of action reducer functions.
* These reducer functions are called with each dispatched action
* and the current or initial state and return a new immutable state.
*/
export const reducers: ActionReducerMap<State> = {
layout: fromLayout.reducer,
router: fromRouter.routerReducer,
};

// app.module.ts
StoreModule.forRoot(reducers),

取自 NgRx 示例应用

最新更新