如何修复NgRx Effects中的执行顺序?

时间:2019-04-03 12:01:09

标签: angular rxjs ngrx ngrx-effects

效果应获取有效载荷并在对话框中显示。 对话框关闭后,它应该发布内容并调度新操作。

问题是,在我关闭对话框之前,动作已被调度。

 @Effect()
  bookTable$ = this.actions$.pipe(
    ofType(BookTableActionTypes.BOOK_TABLE),
    map(action => action.payload),
    exhaustMap((booking: Booking) => this.dialog.open(BookTableDialogComponent, {
      width: this.window.responsiveWidth(),
      data: booking.booking,
    }).afterClosed()
      .pipe( () =>  this.bookTableService.postBooking(booking),
        map((res: any) => new BookTableSuccess(res)),
        catchError(error => of(new BookTableFail({error: error})))
      )));
.pipe( () =>  this.bookTableService.postBooking(booking),
        map((res: any) => new BookTableSuccess(res)),
        catchError(error => of(new BookTableFail({error: error})))
      )));

以上代码仅应在对话框关闭后执行。

1 个答案:

答案 0 :(得分:0)

似乎您在pipe内缺少用于调用API的rxjs运算符。尝试switchMap更改可观察范围。

.pipe( 
   switchMap(() =>  this.bookTableService.postBooking(booking)),
   map((res: any) => new BookTableSuccess(res)),
   catchError(error => of(new BookTableFail({error: error})))
);