在Angular拦截器内调用Http

时间:2018-06-08 10:44:24

标签: angular typescript http rxjs

我的拦截器内部有一个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()阻止。

0 个答案:

没有答案