【问题标题】:Get params from typeOf ngrx effects从 typeOf ngrx 效果中获取参数
【发布时间】:2021-06-25 09:59:36
【问题描述】:

我有这样的事情

ngOnInit() {
  this.store.dispatch(new TutorialActions.GetTutorials(1, 20));
}

export class GetTutorials implements Action {
  readonly type = GET_TUTORIALS
  constructor(public number: number, public offset: number) {}
}

然后我有这样的效果

export class TutorialEffects {
  loadTutorials$ = createEffect(() =>
    this.action$.pipe(
      ofType(TutorialActions.GET_TUTORIALS),
      switchMap(() =>
        this.tutorialService.getAll(0, 20).pipe(
          map((tutorial: Tutorial[]) => new TutorialActions.SuccesGetTutorials(tutorial)),
          catchError((error) => of (error))
        )
      )
    )
  );
}

问题出在ofType(TutorialActions.GET_TUTORIALS)this.tutorialService.getAll(0,20)

该值应该从ngOnInit 函数传递并且可以是动态的?

【问题讨论】:

    标签: angular rxjs observable ngrx


    【解决方案1】:

    是的,您可以访问操作对象。所以你可以访问它的属性。

    export class TutorialEffects {
       loadTutorials$ = createEffect(() =>
         this.action$.pipe(
           ofType(TutorialActions.GET_TUTORIALS),
           switchMap((action: any) =>
             this.tutorialService.getAll(action.number, action.offset).pipe(
               map((tutorial: Tutorial[]) => new TutorialActions.SuccesGetTutorials(tutorial)),
               catchError((error) => of (error))
             )
           )
         )
       );
     }
    

    【讨论】:

      猜你喜欢
      • 2018-06-20
      • 2019-03-18
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 2022-01-05
      • 2018-06-04
      • 2018-04-30
      • 1970-01-01
      相关资源
      最近更新 更多