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

问题描述

我的问题是我有一个装有<router-outlet>lang.component.我需要的是在根组件app.component中访问ActivatedRoute参数,但是那里的所有路由数据都是空的.通过订阅可运行代码的params链接,我通常可以从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;
    }
})

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

10-20 20:14