angular5.x全局loading解决方法

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;

});

});

}