Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Angular 4 HttpInterceptor:显示和隐藏加载器大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经实现了 HttpInterceptor接口,以拦截传出请求和传入响应.

我想在创建请求时显示加载程序,并在收到响应时隐藏该加载程序.

虽然代码波纹管在检测到HttpResponse时起作用,但它没有检测到Http Failure(当响应代码不同于200时),因此加载器不会被隐藏.

@Injectable()
export class AuthInterceptor implements HttpInterceptor {
constructor(private loaderService: LoaderService) { }

intercept(req: HttpRequest<any>,next: HttpHandler): Observable<HttpEvent<any>> {

    this.loaderService.show();

    return next
        .handle(req)
        .do(event => {
            //nothing is printed when a Http failure occurs
            console.log('detecting event ',event);
            if (event instanceof HttpResponse) {
                console.log('detecting http response');
                this.loaderService.hide();
            }
        });
  }
}

解决方法

我建议只是添加一个finally运算符,它可以在成功和错误情况下完成工作:

import 'rxjs/add/operator/finally';

// ...

.finally(()=> this.loaderService.hide())

大佬总结

以上是大佬教程为你收集整理的Angular 4 HttpInterceptor:显示和隐藏加载器全部内容,希望文章能够帮你解决Angular 4 HttpInterceptor:显示和隐藏加载器所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: