0

我想在每个请求开始时实现加载器并在请求完成时隐藏加载器,我已经像这样实现了 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 调用时,拦截方法没有被调用,除此之外我还需要添加任何其他内容吗?我已经关注了这个博客,但我无法处理它

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

例如

当我得到回复时,我想要

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

被执行

4

0 回答 0