reducer 中的 @ngrx/store 和 switch 语句问题

Issue with @ngrx/store and switch statements within reducers

我有以下 两个 @ngrx/store 减速器:

import {ActionReducer, Action} from '@ngrx/store';
import {UserAccount} from '../shared/models/useraccount.model';

export const SET_CURRENT_USER_ACCOUNT = 'SET_CURRENT_USER_ACCOUNT';
export const UPDATE_CURRENT_USER_ACCOUNT_FIRST_NAME = 'UPDATE_CURRENT_USER_ACCOUNT_FIRST_NAME';

export const currentUserAccountReducer: ActionReducer<UserAccount> = (state: UserAccount, action: Action) => {

  console.log('currentUserAccountReducer:', state, action);

  switch (action.type) {
    case SET_CURRENT_USER_ACCOUNT: {
      return action.payload;
    }
    case UPDATE_CURRENT_USER_ACCOUNT_FIRST_NAME: {
      state.firstName = action.payload;
      return state;
    }
  }
};

export const SET_AUTHENTICATED = 'SET_AUTHENTICATED';
export const SET_UNAUTHENTICATED = 'SET_UNAUTHENTICATED';

export const authenticatedReducer: ActionReducer<boolean> = (state: boolean, action: Action) => {

  console.log('authenticatedReducer:', state, action);

  switch (action.type) {
    case SET_AUTHENTICATED: {
      return true;
    }
    case SET_UNAUTHENTICATED: {
      return false;
    }
  }
};

但是,出于某种原因,当我为第一个减速器(即 currentUserAccountReducer)发出调度时,它会更改第二个减速器(即 authenticatedReducer)的状态...

这是导致此问题的调度:

this.store.dispatch({type: SET_CURRENT_USER_ACCOUNT, payload: currentUserAccount});

以下是我在 imports 部分中初始化商店的方式:

StoreModule.provideStore(
 {
   currentUserAccount: currentUserAccountReducer,
   authenticated: authenticatedReducer
 })

有人可以提供建议吗?

编辑:问题是 authenticated 最终变成了 undefined!!

reducer 中的 switch 语句不包含 default 个案例。您需要添加 default 个 return state 的案例,因为 reducers 将被调用以进行 all 操作 - 商店无法知道应该为特定的操作类型调用哪个 reducer,因此每个分派的操作都会传递给每个 reducer。