我的拦截器内部有一个http调用:
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
const apiConfigService = this.injector.get(ApiConfigService);
const pingService = this.injector.get(PingService);
let changedReq;
let changedUrl;
return pingService.ping().pipe(switchMap((data) => {
apiConfigService.changeNetwork(data.type);
changedUrl = apiConfigService.changeUrl(req.url);
changedReq = req.clone({ url: changedUrl });
return next.handle(changedReq);
}),
catchError((err) => {
changedUrl = apiConfigService.changeUrl(req.url);
changedReq = req.clone({ url: changedUrl });
return next.handle(changedReq);
}));
}
每次http调用都会引发catchError
失败,而不仅是pingService.ping()
错误。我该如何避免这种行为?我只想在catchError
失败时触发ping()
阻止。