现在对于ngrx store用户推荐的是在reducer中使用"on()"switch语句的语法。它看起来像下面(docs):
export const scoreboardReducer = createReducer(
initialState,
on(ScoreboardPageActions.homeScore, state => ({ ...state, home: state.home + 1 })),
on(ScoreboardPageActions.awayScore, state => ({ ...state, away: state.away + 1 })),
on(ScoreboardPageActions.resetScore, state => ({ home: 0, away: 0 })),
on(ScoreboardPageActions.setScores, (state, { game }) => ({ home: game.home, away: game.away }))
);
我想控制台日志所有的行动,以减少。我如何在这样的语法中方便地做到这一点(我有这么多动作)?使用旧的switch case语句,我可以简单地通过:
export function scoreboardReducer(state = initialState, action: ScoreboardAction){
console.log('action', action)
switch(action.type){
// all handlers
}
}
这是不可能的了:(
你可以这样做:
export function scoreboardReducer(state = initialState, action: ScoreboardAction){
console.log('action', action)
return reducerThatsUsingOn(state, action)
}