无法使用angular6调用HTTP_INTERCEPTORS

时间:2019-03-21 09:31:42

标签: angular angular6 angular-http-interceptors angular-httpclient-interceptors

我想在每个请求开始时实现加载程序,并在请求完成时隐藏加载器,我已经实现了HTTP_INTERCEPTORS

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class HttpConfigInterceptor implements HttpInterceptor {
    constructor() { }
    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        return next.handle(request).pipe(
            map((event: HttpEvent<any>) => {
                if (event instanceof HttpResponse) {
                    debugger;
                    console.log('event--->>>', event);                }
                return event;
            }));
    }
}

我已经这样在 app.module.ts 中注册了HTTP_INTERCEPTOR

 providers: [ 
    { provide: HTTP_INTERCEPTORS, useClass: HttpConfigInterceptor, multi: true}
  ]

但是当我进行HTTP调用时,没有调用 intercept方法时,是否还需要添加其他内容? 我已遵循此blog,但无法处理

拦截方法正在启动,但是当做出响应或请求时,不会调用适当的方法。

  

例如

当我收到回复时,我会想要

if (event instanceof HttpResponse) {
                    debugger;
                    console.log('event--->>>', event);                }
                return event;
            }

被执行

0 个答案:

没有答案