This is my routers define.
export const routes: Routes = [
{
path: '',
component: LayoutComponent,
children: [
{
path: 'sys',
loadChildren: () => import('./pages/sys/sys.routers').then(m => m.SYS_ROUTES),
canActivate: [unAuthGuard]
}
]
},
];
This is the SYS_ROUTRES on /pages/sys/sys.routers
.
export const SYS_ROUTES: Routes = [
{ path: 'menu', component: MenuManageComponent, data: {title: '菜单管理'} },
];
I want get the router data in LayoutComponent. Here is my code.
export class LayoutComponent implements OnInit {
constructor(private router: Router, private activatedRoute: ActivatedRoute) {
}
ngOnInit(): void {
// Direct access, not trigger router events.
this.activatedRoute.data.subscribe(data => {
console.log(data); // undefined
})
console.log(this.activatedRoute.snapshot.data); // undefined
this.router.routerState.root.data.subscribe(data => {
console.log(data); // undefined
})
console.log(this.router.routerState.snapshot.root.data); // undefined
}
}
When I route to sys/menu
. The above get ways is always undefined
.
Neither Router
nor ActivatedRoute
can correctly obtain the router data.
How can I get the router data?