#angular #rxjs #observable #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
функций и может быть динамическим?
Ответ №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))
)
)
)
);
}