如何在Angular 5中显示每个HTTP请求的微调框? [英] How to Show spinner for every HTTP requests in angular 5?
问题描述
我对角度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 为您的所有请求显示微调框,这是一个很好的
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() );
}
享受!
奖金:这是一个旋转服务实现示例
这篇关于如何在Angular 5中显示每个HTTP请求的微调框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!