簡體   English   中英

如何在ngrx存儲中使用單個操作更新多個Reducer的狀態?

[英]How to update states of multiple reducers with a single action in ngrx store?

我的應用程序中有多個reducers,它們組合在app.reducers.ts中。 當我的動作發生時,一個reducer的狀態應該從另一個reducer的當前狀態更新為單個值(因此計划外交付將被調度),然后從第二個reducer的狀態中刪除該值。 這是我如何刪除它,但在此之前我需要更新其他狀態。

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

從應用程序的角度來看,調用2個動作非常糟糕。 所以我需要以某種方式在更高層次上處理這個問題。

您的商店可以有多個減速器,每個減速器分別對動作起作用。

例如,您可以將它們定義為

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 (...)
....

然后將所有reducers傳遞給模塊中的ngrx

StoreModule.forRoot(reducers)

舉個好例子,看看這個回購

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM