Accessing Route data in root component, route data is empty

白昼怎懂夜的黑 提交于 2019-12-01 16:43:09

问题


My problem is that I have a lang.component loaded with <router-outlet>. What I need is to access ActivatedRoute params in the root component which is app.component but all route data there is empty. I am able to access route data from lang.component normally by subscribing to params link to code that you can run: https://plnkr.co/edit/gTXPUb6TvatbMyOPPRKj

If you run the code and open your console you can see two logs. It shows you what the problem is.

Please help.

Thank you.


回答1:


Instead of using ActivatedRoute use Router.

this.router.events.subscribe((event) => {
    if(event instanceof NavigationEnd) {
        var snapshot = this.router.routerState.firstChild(this.route).snapshot;

        var params = snapshot.params;
    }
})


来源:https://stackoverflow.com/questions/38460470/accessing-route-data-in-root-component-route-data-is-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!