StoreModule.forRoot() - 如何在没有附加键的情况下返回对象

问题描述 投票:1回答:1

我想知道如何返回与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),但后来我得到空的物体,它不起作用。

ngrx ngrx-store reducers
1个回答
0
投票

StoreModule上的forRoot方法需要和ActionReducerMap,而不是reducer的结果。

我通常在一个单独的文件中设置如下:

export interface IAppState {
    aPieceOfState: IAPieceOfState;
}

export const reducers: ActionReducerMap<IAppState> = {
    aPieceOfState: aPieceOfStateReducer
};

然后将其导入app.module.ts并使用它:

StoreModule.forRoot(reducers)
© www.soinside.com 2019 - 2024. All rights reserved.