访问根组件中的路由数据,路由数据为空 [英] Accessing Route data in root component, route data is empty

查看:17
本文介绍了访问根组件中的路由数据,路由数据为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我有一个 lang.component 加载了 .我需要的是访问 app.component 根组件中的 ActivatedRoute 参数,但那里的所有路由数据都是空的.我可以通过订阅您可以运行的代码的参数链接来访问 lang.component 的路由数据:https://plnkr.co/edit/gTXPUb6TvatbMyOPPRKj

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.

请帮忙.

谢谢.

推荐答案

不要使用 ActivatedRoute,而是使用 Router.

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;
    }
})

这篇关于访问根组件中的路由数据,路由数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆