首页 > 解决方案 > 从 typeOf ngrx 效果中获取参数

问题描述

我有这样的东西

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函数传递并且可以是动态的?

标签: angularrxjsobservablengrx

解决方案


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

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))
         )
       )
     )
   );
 }

推荐阅读