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


問題描述

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

我正在嘗試將 id 參數從調度發送到效果,我在谷歌中找不到這種情況的任何示例。

這是我已經擁有的代碼:

組件:

 ngOnInit(): void {
   this.packageClass = `type‑${this.package.packageType.toLowerCase()}`;
   // I set the payload to the action
   this.store.dispatch(new LoadClusterInfo({id: this.package.id}));
   this.checkStatus();
 }

效果(我需要訪問值的地方)

@Effect()
getClusterInfo = 
  this.actions.ofType(resultActions.Type.LOAD_CLUSTER_INFO)
    .pipe(
      switchMap(() => {
        let id = 'HARDCODED_ID';
        return this.service.getPackageCluster(id); // Here is where i need the value
      }),
      map((packageCluster: PackageCluster) => new LoadClusterInfoSuccess(packageCluster)),
      catchError((err: Error) => of(new LoadClusterInfoError(err))),
    );

最後一個動作:

  export class LoadClusterInfo implements Action {
    readonly type = Type.LOAD_CLUSTER_INFO;
    constructor(readonly payload: any) {}
  }

如何訪問id由組件(this.package.id)發送的效果?


參考解法

方法 1:

You can access the action's payload property in the switchMap operator. A couple of extra things:

  • Use the pipeable ofType operator because the ofType function is removed in NgRx 7
  • Type the ofType operator to have a typed action
  • use map and catchError on the service stream, otherwise when an error occurs the effect stream will get destroyed. See the NgRx docs for more info.

  • </ul>

    @Effect()
      getClusterInfo = this.actions
      .pipe(
        ofType<LoadClusterInfo>(resultActions.Type.LOAD_CLUSTER_INFO),
        switchMap((action) => {
          return this.service.getPackageCluster(action.id).pipe(
            map((packageCluster: PackageCluster) => new LoadClusterInfoSuccess(packageCluster)),
            catchError((err: Error) => of(new LoadClusterInfoError(err))),
         ); 
        }),  
      );
    

    Update NgRx v8 +

    With createAction and createEffect, the action is automatically inferred so you can do this and benefit from the types:

    getClusterInfo = createEffect(() => {
      return this.actions.pipe(
        ofType(loadClusterInfo),
        switchMap((action) => {
          return this.service.getPackageCluster(action.id).pipe(
            map((packageCluster: PackageCluster) => new LoadClusterInfoSuccess(packageCluster)),
            catchError((err: Error) => of(new LoadClusterInfoError(err))),
         ); 
        }),  
      )
    }
    

    方法 2:

    For those trying to get params with mergeMap you can do it like these:

     loadItems$ = createEffect(() =>
            this.actions$.pipe(
              ofType(YOUR_ACTION_NAME),
              mergeMap((action) =>
                this.myService.getAll(action.parameters).pipe(
                  map((items) => ({
                    type: ITEMS_LOADED_SUCESSFULLY,
                    items: itemsList,
                  })),
                  catchError(() => EMPTY)
                )
              )
            )
          );
    

    To dispatch the action:

    this.store.dispatch(loadItems({ parameters: parameter }))
    

    And in your .actions.ts:

    export const loadItems = createAction(
    YOUR_ACTION_NAME, props<{ parameters: string }>());
    

    (by nperez9timdeschryverDylan Moguilevsky)

    參考文件

    1. Angular NgRx effects, how to pass a parameter? (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)







留言討論