Angular ActivatedRoute data returns an empty object

后端 未结 5 1431
时光说笑
时光说笑 2020-12-15 02:43

I have a route registered with some data:

const routes: Routes = 
[
    {path: \'my-route\', data: { title: \'MyTitl         


        
5条回答
  •  心在旅途
    2020-12-15 03:19

    This is part of my code (NOTE: I'm using Angular 8):

    constructor(private _router: Router, private _route: ActivatedRoute) {}
    
    ngOnInit() {
      ...
      this.listenRouting();
    }
    
    listenRouting() {
        this._router.events.subscribe((router: any) => {
          routerUrl = router.url;
          if (routerUrl && typeof routerUrl === 'string') {
            routerList = routerUrl.slice(1).split('/');
            console.log("data.breadcrumbs", this._route.snapshot.routeConfig.children.find(child => child.path === routerList[routerList.length - 1]).data.breadcrumbs);
            ...
          }
        });
    }
    

    So data is "hiding" under ActivatedRoute.snapshot.routeConfig.children Array. Each child contains, among other things, data. In my case data is configured in routing.module, e.g.:

    const routes: Routes = [
      { path: "facility-datas",
        component: FacilityTerminal,
        data: {
          breadcrumbs: "b ft"
        }
      },
    ...
    ];
    

提交回复
热议问题