如何取消在 RXJS 效果器中发出的角度 http 请求



我想取消在角度 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);
}));
}));

请让我知道如何使用角度 8 做同样的事情。另请注意,我将无法使用切换映射,因为多个小部件角度组件将使用不同的有效负载调用此操作。

我们可以通过使用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);
}));
}));

最新更新