我在我的应用程序路由模块中定义了一些路由数据,如下所示:
- const appRoutes:Routes = [
- {path: '',component: LoginComponent,data:[{PageName:"Login Page"}]}]
我想全局获取数据,所以我使用app.component.ts来获取URL重定向相关信息,如下所示:
- export class AppComponent {
- title = 'Welcome';
- constructor(public router: Router,public authenticationService: AuthenticationService) {
- this.router.events.subscribe(event => {
- console.log("Url",event.urlAfterRedirects);
- console.log("Page Name",router[PageName]); //Not working
- }
我尝试注入ActivatedRoute,但每次重定向后都没有更新.
解决方法
尝试过滤和循环您的事件而不是订阅
- constructor(router:Router,route:ActivatedRoute) {
- router.events
- .filter(e => e instanceof NavigationEnd)
- .forEach(e => {
- this.title = route.root.firstChild.snapshot.data['PageName'];
- });
- }