Ionic 3 ngrx / store减速器仅在生产模式下不接收调度的动作

时间:2018-10-18 03:15:09

标签: angular ionic-framework redux ionic3 ngrx-store

我有一个 Ionic 3应用程序,该应用程序使用 ngrx / store 使用网络套接字 redux 。首先,我的所有代码在开发模式下都可以正常工作。在浏览器和真实设备中。

但是当我尝试以生产模式构建它时。该操作仍在分派,但减速器未收到已分派的操作,导致应用程序的状态未更新。

这是我的减速器下面的代码。

import { Action } from '@ngrx/store';

const UPDATE_AVATAR = '[Websokcet] New ORDER';
type Type = UpdateAvatar

export class UpdateAvatar implements Action {
  readonly type = UPDATE_AVATAR;
  constructor(public payload: any) { }
}

export function UpdateAvatarReducer(state: any, action: Type) {
  console.log('ACTION RECEIVED:', state, action)
  switch (action.type) {
    case UPDATE_AVATAR:
      return action.payload;
  }
}

和我的 rootReducers

import { UpdateAvatar, UpdateAvatarReducer } from './reducers/uploadAvatar';

export function rootReducer () {
  return {
    reducers: {
      driverUpdateProfile: DriverUpdateProfileReducer,
    },
  }
}

和我的 app.module.ts

import { rootReducer } from '../store/websocket';

// and in the **imports arrays**

StoreModule.forRoot({
  ...rootReducer().reducers
}),

它可以在开发模式下工作,但不能在生产中使用。为什么?

感谢有人可以帮助您。 预先感谢。

1 个答案:

答案 0 :(得分:4)

我设法通过更改实现 rootReducer rootActions 的方式来解决问题。我没有导出函数,而是返回了声明的2个单独的对象。

这是我的 rootReducer 操作

下方的旧代码
export default function () {
  return {
    reducers: {
      newLocation: NewLocationReducer,
      newOrder: NewOrderReducer,
      orderTaken: OrderTakenReducer,
      driverUpdateProfile: DriverUpdateProfileReducer,
      driverUpdateAvatar: UpdateAvatarReducer,
      newTransaction: NewTransactionReducer,
      updateTransaction: UpdateTransactionReducer,
      orderNewMessage: OrderNewMessageReducer,
    },
    actions: {
      newLocation: NewLocation,
      newOrder: NewOrder,
      orderTaken: OrderTaken,
      driverUpdateProfile: DriverUpdateProfile,
      driverUpdateAvatar: UpdateAvatar,
      newTransaction: NewTransaction,
      updateTransaction: UpdateTransaction,
      orderNewMessage: OrderNewMessage,
    }
  }
}

这是下面的新代码:

export const rootActions = {
  newLocation: NewLocation,
  newOrder: NewOrder,
  orderTaken: OrderTaken,
  driverUpdateProfile: DriverUpdateProfile,
  driverUpdateAvatar: UpdateAvatar,
  newTransaction: NewTransaction,
  updateTransaction: UpdateTransaction,
  orderNewMessage: OrderNewMessage,
}

export const rootReducer = {
  newLocation: NewLocationReducer,
  newOrder: NewOrderReducer,
  orderTaken: OrderTakenReducer,
  driverUpdateProfile: DriverUpdateProfileReducer,
  driverUpdateAvatar: UpdateAvatarReducer,
  newTransaction: NewTransactionReducer,
  updateTransaction: UpdateTransactionReducer,
  orderNewMessage: OrderNewMessageReducer,
}

我将它们声明为两个单独的变量,我认为它是更有条理的,而不是旧变量。