1

我有以下两个@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

4

1 回答 1

4

您的减速器中的switch语句不包含default案例。您需要添加default返回 的案例,因为所有state操作都会调用减速器- 存储无法知道应该为特定操作类型调用哪个减速器,因此每个调度的操作都会传递给每个减速器。

于 2016-08-20T18:59:22.947 回答