您可以订阅
Router.events
https://angular.io/guide/router#router-events
路由器配置启动
在路由器延迟加载路由配置之前触发的事件。
已配置路由
延迟加载路由后触发的事件。
假设你有以下懒惰的路线:
{
path: 'lazy', loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)
}
下面是如何连接到这个模块的加载过程:
import { RouteConfigLoadEnd, RouteConfigLoadStart, Router } from '@angular/router';
...
constructor(private router: Router) {
router.events.subscribe((
event => {
if (event instanceof RouteConfigLoadStart && event.route.path === 'lazy') {
console.log('START');
}
if (event instanceof RouteConfigLoadEnd && event.route.path === 'lazy') {
console.log('FINISH');
}
}));
}