Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing Route data in root component, route data is empty

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.

like image 544
bobek Avatar asked Jul 19 '16 13:07

bobek


People also ask

What is the difference between ActivatedRoute and ActivatedRouteSnapshot?

Since ActivatedRoute can be reused, ActivatedRouteSnapshot is an immutable object representing a particular version of ActivatedRoute . It exposes all the same properties as ActivatedRoute as plain values, while ActivatedRoute exposes them as observables.

What is the use of ActivatedRoute?

ActivatedRoutelink. Provides access to information about a route associated with a component that is loaded in an outlet. Use to traverse the RouterState tree and extract information from nodes.


1 Answers

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;
    }
})
like image 137
bobek Avatar answered Oct 17 '22 03:10

bobek