如何在ngrx存储中通过单个操作更新多个reducer的状态? [英] How to update states of multiple reducers with a single action in ngrx store?

查看:184
本文介绍了如何在ngrx存储中通过单个操作更新多个reducer的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有多个减速器,这些减速器组合在app.reducers.ts中.当我的动作发生时,应使用另一个减速器的当前状态中的单个值来更新一个减速器的状态(这样,计划外的交货将成为已计划的),然后从第二个减速器的状态中删除该值. 这是我可以删除它的方法,但是在执行此操作之前,我需要更新其他状态.

I have multiple reducers in my application which are combined in app.reducers.ts. When my action happens state of one reducer should be update with single value from current state of the other reducer(so unplanned delivery will become scheduled) and then remove that value from state of second reducer. Here is how I can delete it, but before doing this I need to update other state.

case UNPLANNED_ACTIONS.REMOVE_UNPLANNED_DELIVERY:
        return Object.assign({}, state, {
            deliveries: state.deliveries.filter(delivery => delivery.deliveryId !== payload.deliveryId)
        });

从应用程序的角度来看,调用2个动作非常糟糕.因此,我需要以某种方式在更高级别上进行处理.

Calling 2 actions is very bad from application point of view. So I need to somehow handle this on the higher level.

推荐答案

您的商店可以有多个减速器,每个减速器分别作用于该动作.

Your store can have multiple reducers, with each acting separately on the action.

例如,您可以将它们定义为

For example, you can define them like

export function reducer(
  state = initialState,
  action: fromMyStoreActions.DeliveryAction
): AppState{

  switch (action.type) {
    case UNPLANNED_ACTIONS.REMOVE_UNPLANNED_DELIVERY:
         return Object.assign({}, state, {
              deliveries: state.deliveries.filter(delivery => delivery.deliveryId !== payload.deliveryId)
         });
    ...
}

// Another Reducer
export function reducer(
      state = initialState,
      action: fromMyStoreActions.DeliveryAction
    ): AppState{

      switch (...)
....

然后将所有的reducer传递给模块中的ngrx,例如

And then pass all the reducers to ngrx in the module like

StoreModule.forRoot(reducers)

例如,请参见此仓库

这篇关于如何在ngrx存储中通过单个操作更新多个reducer的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆