saveQuiz$ = createEffect(() =>
this.actions$.pipe(
ofType(quizActions.addQuiz),
map(action => action.saveQuiz),
switchMap((quiz) => this.quizDataService.postQuiz(quiz)),
tap((quizId) => console.log('NEED THE ACTION PAYLOAD HERE')),
map((quizId:number) => quizActions.addQuizSuccess({quiz:{ id:quizId }})),
catchError(() => EMPTY)
)
);
这是我创建的效果,问题是我需要在action.payload
之后访问switchmap (which calls the api)
。我有什么办法吗?
您可以执行以下操作:
import { combineLatest, of } from 'rxjs';
.....
saveQuiz$ = createEffect(() =>
this.actions$.pipe(
ofType(quizActions.addQuiz),
map(action => action.saveQuiz),
switchMap((quiz) => { return combineLatest(of(quiz), this.quizDataService.postQuiz(quiz)); }),
tap(([quizAction, quizId]) => console.log(`${quizAction}, ${quizId}`)),
map(([, quizId]) => quizActions.addQuizSuccess({quiz:{ id:quizId }})),
catchError(() => EMPTY)
)
);