使用ngrx在Angular中进行状态管理时,控制台记录状态数据

时间:2018-10-11 18:58:41

标签: ngrx ngrx-store

在使用ngrx进行角度应用程序中的状态管理时,谁能建议如何控制台记录状态。我已经遍历了ngrx-store-logger,但是该文档尚不清楚如何创建元缩减器并使用此库。

1 个答案:

答案 0 :(得分:3)

这可以使用meta reducer来完成,如NgRx example app

所示
export function logger(reducer: ActionReducer<State>): ActionReducer<State> {
  return (state: State, action: any): any => {
    const result = reducer(state, action);
    console.groupCollapsed(action.type);
    console.log('prev state', state);
    console.log('action', action);
    console.log('next state', result);
    console.groupEnd();

    return result;
  };
}

/**
 * By default, @ngrx/store uses combineReducers with the reducer map to compose
 * the root meta-reducer. To add more meta-reducers, provide an array of meta-reducers
 * that will be composed to form the root meta-reducer.
 */
export const metaReducers: MetaReducer<State>[] = !environment.production
  ? [logger, storeFreeze]
  : [];