import { Router, ActivatedRoute, NavigationEnd, NavigationStart } from '@angular/router';
private allLoading: boolean = false;
constructor(
private router: Router,
private activatedRoute: ActivatedRoute
) {
setTimeout(() => {
this.routerChange();
});
}
public routerChange() {
this.router.events
.filter(event => event instanceof NavigationStart)
.map(() => this.activatedRoute)
.map((route: any) => {
while (route.firstChild) route = route.firstChild;
return route;
})
.subscribe((event) => {
this.allLoading = true;
this.router.events
.filter(event => event instanceof NavigationEnd)
.map(() => this.activatedRoute)
.map((route: any) => {
while (route.firstChild) route = route.firstChild;
return route;
})
.subscribe((event) => {
this.allLoading = false;
});
});
}
Angular路由加载状态管理
本文介绍了一种在Angular应用中管理页面加载状态的方法,通过监听路由事件,在页面切换开始时设置加载状态为true,在页面加载完成后设置为false,实现全局加载效果的统一控制。
667

被折叠的 条评论
为什么被折叠?



