如何为角度 5 中的每个 HTTP 请求显示微调器? [英] How to Show spinner for every HTTP requests in angular 5?

查看:24
本文介绍了如何为角度 5 中的每个 HTTP 请求显示微调器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 angular 5 的新手.如何编写一个通用函数以在 angular 5 中为每个 HTTP 请求显示微调器.请帮助我实现这一点.

i am new to angular 5 . How to code a common function to show spinner for every HTTP request in angular 5.Please help me to implement this.

推荐答案

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

You can use Angular HttpInterceptor to show a spinner for all your requests, Here's a good medium article on how to implement an http interceptor

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

Also, you will have to create a spinner service/module and inject it in your http interceptor. Finally, in your intercept method you can use the finally rxJs method to stop your spinner. Here's a simple implementation:

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

享受吧!

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

这篇关于如何为角度 5 中的每个 HTTP 请求显示微调器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆