有没有人知道如何在组件加载时第一次触发路由器事件?
我正在使用 Angular 8。
我有一个深链接,可以移动到另一个组件。所以当我点击深链接时,它会重定向到主页。这种情况发生在计算机启动新会话时。
因此,根据我的调查,当应用程序第一次加载时,(this.router.events)不返回任何内容。
请问有人能给我提供相关建议吗?
以下是我的代码:- app.component.ts
我正在使用 Angular 8。
我有一个深链接,可以移动到另一个组件。所以当我点击深链接时,它会重定向到主页。这种情况发生在计算机启动新会话时。
因此,根据我的调查,当应用程序第一次加载时,(this.router.events)不返回任何内容。
请问有人能给我提供相关建议吗?
以下是我的代码:- app.component.ts
ngAfterViewInit() {
this.getActivatedRoute();
}
getActivatedRoute() {
debugger
const routerSub$ = this.router.events.subscribe((event) => {
if (event instanceof NavigationEnd) {
this.routes = event.url.toLowerCase().split('/');
this.currentRoute = this.routes[1];
if (this.currentRoute !== this.previousRoute) {
this.setBodyClassBasedOnRouter(this.routes);
this.previousRoute = this.currentRoute;
}
}
});
}