切換地圖後在NGRX特效點擊功能中獲取動作道具 (Get action props in the NGRX effects tap function after switch Map)


問題描述

切換地圖後在NGRX特效點擊功能中獲取動作道具 (Get action props in the NGRX effects tap function after switch Map)

我在 Angular 中有一個效果很好,但我希望在調用 switch 映射中的 API 後傳遞 action prop 變量。

這是效果

    updateCustomer$ = createEffect(() =>
    this.actions$.pipe(
        ofType(updateCustomer),
        switchMap(action =>
            this.customerService.updateCustomer(action.customer).pipe(
                map(response => {
                    return successLoad({ data: response });
                }),
                catchError(EMPTY)
            )
        ),
        tap((action) => {
            console.log(action); I want to get props that I passed in the switch map. I need the value of flag that I passed so I can do some other things based on the flag
            //Do something
        })
    )
    );

Action


export const updateCustomer = createAction(
    '[Component] Add Customer',
    props<{ customer: customer, flag:string}>()
);

我想得到我在開關地圖中傳遞的道具。我需要我傳遞的標誌值,以便我可以根據點擊運算符中的標誌做一些其他事情。這可能嗎?


參考解法

方法 1:

It is possible, but workflow depends on what you want to do with it. Do you want to dispatch another action in the tap, or you just want to do some other stuff? If it's only for other stuff then map your action and updateCustomer response to the object and process it in the following map operator. That way your effect will still return the successLoad action and you have an access to

updateCustomer$ = createEffect(() =>
  this.actions$.pipe(
  ofType(updateCustomer),
  switchMap(action =>
    this.customerService.updateCustomer(action.customer).pipe(
      map(response => ({ action, response })),
      catchError(EMPTY)
    )
  ),
  //filter(response => !!response) // might neeed to add a filter for EMPTY response, not sure if !! is enough
  map(({action, response}) => {
    console.log(action); // your first action is here for some magic
    return successLoad({ data: response }); // return new action with data from updateCustomer
  }),
  filter(action => !!action) // failsafe is `updateCustomer` fails
);

If you want to return multiple actions, then replace the last map with switchMap and return the array of actions.

(by Learn AspNetmat.hudak)

參考文件

  1. Get action props in the NGRX effects tap function after switch Map (CC BY‑SA 2.5/3.0/4.0)

#ngrx-effects #ngrx #Angular






相關問題

rxjs減少不繼續 (rxjs reduce does not continue)

從 reducer/action 觸發 API 重新加載的正確方法是什麼? (What's the proper way to trigger the API reload from reducer/action?)

Angular NgRx 效果,如何傳遞參數? (Angular NgRx effects, how to pass a parameter?)

NgRx 效果無限循環 (NgRx Effects infinite loop)

發送 forkjoin api,每個 api 之間有延遲 (Send forkjoin api with a delay in between each api)

如何從所有待處理的請求中取消特定請求 (How to cancel specific request from all pending request)

使用 NgRx 存儲優化 Angular 內存以避免內存洩漏 (Angular memory optimization with NgRx store to avoid memory leaks)

NgRx EffectsModule 導入組 (NgRx EffectsModule imports group)

切換地圖後在NGRX特效點擊功能中獲取動作道具 (Get action props in the NGRX effects tap function after switch Map)

如何防止在 NgRx 中第二次加載數據? (How prevent loading data second time in NgRx?)

需要幫助了解這種 Ngrx 效果 (Need help understanding this Ngrx effect)

測試效果 NgRx Angular 11 (Testing effects NgRx Angular 11)







留言討論