如何取消 RXJS Effects 中发出的角度 http 请求

Rah*_*ase 3 typescript ngrx-effects ngrx-store angular8 rxjs-observables

我想取消 Angular 8 中 RXJS 效果中发出的 http 请求。

@Effect() getReport$ = this.action$.pipe(
ofType(ActionTypes.GET_WIDGET),   
map(toPayload),
mergeMap(payload => {
  return this.dashboardService.getReport(payload).pipe(
    map(this.extractData),
    switchMap(result => {
      return observableOf(<ReceivedWidgetAction>{
        type: ActionTypes.RECEIVED_WIDGET,
        payload: result
      });
    }),
    catchError((err: Response | any) => {
      return this.handleReportError(err);
    }));
    
}));
Run Code Online (Sandbox Code Playgroud)

请让我知道如何使用 Angular 8 执行相同操作。另请注意,我将无法使用切换映射,因为多个 Angular 组件将使用不同的有效负载调用此操作。

Rah*_*ase 9

我们可以通过使用 takeUnitil 运算符并创建新操作并在需要取消请求时使用存储分派相同的操作来取消正在进行的请求。

 @Effect() getReport$ = this.action$.pipe(
ofType(ActionTypes.GET_WIDGET),   
map(toPayload),
mergeMap(payload => {
  return this.dashboardService.getReport(payload).pipe(
    map(this.extractData),
    switchMap(result => {
      return observableOf(<ReceivedWidgetAction>{
        type: ActionTypes.RECEIVED_WIDGET,
        payload: result
      });
    }),
    takeUntil(this.action$.pipe(ofType(DashboardActionTypes.CANCEL_REQUEST))),
    catchError((err: Response | any) => {
      return this.handleReportError(err);
    }));

}));
Run Code Online (Sandbox Code Playgroud)

  • 取消后,我似乎无法再次发送`GET_WIDGET`。该效果不再被调用。 (2认同)