Angular 4.3 HttpClient : Intercept response

后端 未结 4 1204
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 12:59

In the documentation about the new HttpClientModule included in the new version of Angular 4.3, the mechanism to intercept requests is explained very well. Ther

4条回答
  •  天命终不由人
    2020-12-02 13:19

    From what i can understand (I've only done the intercept for request and inject auth token) .. you can attach a .do() and test if is a reponse .. like (as doc says):

    import 'rxjs/add/operator/do';
    
    export class TimingInterceptor implements HttpInterceptor {
      constructor(private auth: AuthService) {}
    
      intercept(req: HttpRequest, next: HttpHandler): Observable> {
        const started = Date.now();
        return next
          .handle(req)
          .do(event => {
            if (event instanceof HttpResponse) { //<-- HERE
              const elapsed = Date.now() - started;
              console.log(event} ms.`);
            }
          });
      }
    
    }
    

提交回复
热议问题