我正在使用一个breadcrumbs组件来订阅router.events.问题是第一次加载组件时,它不会触发订阅.
ngOnInit(): void {
console.log("oninit beradcumbs");
this.router.events.filter(event => event instanceof NavigationEnd).subscribe(event => {
this.breadcrumbs = [];
let currentRoute = this.route.root,
url = '';
do {
const childrenRoutes = currentRoute.children;
currentRoute = null;
childrenRoutes.forEach(route => {
if (route.outlet === 'primary') {
const routeSnapshot = route.snapshot;
url += '/' + routeSnapshot.url.map(segment => segment.path).join('/');
this.breadcrumbs.push({
label: route.snapshot.data,
url: url
});
currentRoute = route;
}
});
} while (currentRoute);
console.log("Breadcumbs",this.breadcrumbs);
});
}
Run Code Online (Sandbox Code Playgroud)
单击具有routerLink属性的链接时,订阅将发出值,并且工作正常.我将我的项目与使用Augury的工作项进行了比较,并且在面包屑组件中,路由器的状态如下所示:不工作的路由器
工作的一个:工作路由器
现在让我,如果您需要更多信息帮助:)提前感谢!