在ngrx Effect中访问switchmap(api call)后的action payload

Access the a action payload after switchmap(api call) in ngrx Effect

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

这是我创建的效果,问题是我需要在 switchmap (which calls the api) 之后访问 action.payload。有什么办法可以做到吗?

你可以这样做:

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