在ngrx效果中的switchmap(api调用)之后访问动作有效负载

问题描述 投票:0回答:1
 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)。我有什么办法吗?

angular ngrx ngrx-effects
1个回答
0
投票

您可以执行以下操作:

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)
    )
  );
© www.soinside.com 2019 - 2024. All rights reserved.