自己写了一个loading-bar插件,不过都是手动调用和关闭,每个http请求都得手动添加自定义插件,想问问有没有人用过好用的loading-bar,只要调用一次,就能全局监听http请求,当用户网络不太好的时候就会有加载的动画出现?谢谢!
自己写了一个loading-bar插件,不过都是手动调用和关闭,每个http请求都得手动添加自定义插件,想问问有没有人用过好用的loading-bar,只要调用一次,就能全局监听http请求,当用户网络不太好的时候就会有加载的动画出现?谢谢!
题主,您好,
本人也是采用 @mafeifan 同学 提到的那个插件。以下是我自己的实践,希望能有所帮助。app.component.ts
如下:
import { Component, OnDestroy } from '@angular/core';
import { SlimLoadingBarService } from "ng2-slim-loading-bar";
import { NavigationCancel, NavigationEnd, NavigationError, NavigationStart, Router } from "@angular/router";
@Component({
// tslint:disable-next-line
selector : 'body',
templateUrl: './app.component.html'
})
export class AppComponent implements OnDestroy {
private sub: any;
constructor(private slimLoader: SlimLoadingBarService, private router: Router) {
// Listen the navigation events to start or complete the slim bar loading
this.sub = this.router.events.subscribe(event => {
if (event instanceof NavigationStart) {
this.slimLoader.start();
} else if (event instanceof NavigationEnd ||
event instanceof NavigationCancel ||
event instanceof NavigationError) {
this.slimLoader.complete();
}
}, (error: any) => {
this.slimLoader.complete();
});
}
ngOnDestroy(): any {
this.sub.unsubscribe();
}
}
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答2.3k 阅读✓ 已解决
3 回答2.1k 阅读✓ 已解决
查了一些资料后在http加入了监听,放在 https://github.com/ChenJunhan...上了