如何在角度5中显示每个HTTP请求的微调器?

Har*_*ish 5 angular-http angular-http-interceptors angular angular-httpclient

我是角度5的新手.如何编写一个通用函数来为角度为5的每个HTTP请求显示微调器.请帮我实现这个.

Meh*_*oha 12

您可以使用Angular HttpInterceptor为您的所有请求显示一个微调器,这是一篇关于如何实现http拦截器的好文章.

此外,您将必须创建一个微调器服务/模块并将其注入您的http拦截器.最后,在你的拦截方法中,你可以使用finally rxJs方法来阻止你的微调器.这是一个简单的实现:

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.spinnerService.start();
    return next.handle(req).finally(res => this.spinnerService.stop() );
  }
Run Code Online (Sandbox Code Playgroud)

请享用 !

额外奖励:这是一个微调服务实现示例