我想知道如何返回与reducer函数相同类型的对象:
function storeReducer(
state = INITIAL_APPLICATION_STATE,
action: Actions
): ApplicationState {
switch (action.type) {
case LOAD_USER_THREADS_ACTION:
return handleLoadUserThreadsAction(state, action);
default:
return state;
}
}
我期望ApplicationState类型的对象,但使用这种方法:
StoreModule.forRoot({storeReducer})
我正在使用密钥获取对象:
storeReducer:{ // object of type Application State}
我期待得到对象(没有额外的storeReducer键):
{//object of type Application State}
也试过StoreModule.forRoot(storeReducer)
,但后来我得到空的物体,它不起作用。
StoreModule上的forRoot方法需要和ActionReducerMap,而不是reducer的结果。
我通常在一个单独的文件中设置如下:
export interface IAppState {
aPieceOfState: IAPieceOfState;
}
export const reducers: ActionReducerMap<IAppState> = {
aPieceOfState: aPieceOfStateReducer
};
然后将其导入app.module.ts并使用它:
StoreModule.forRoot(reducers)