3

I have a module with the following routing:

{
    path: '',
    component: PagesComponent,
    children: [
      {
        data: { test: 'snoop' },
        path: 'dashboard',
        loadChildren: './modules/dashboard/dashboard.module#DashboardModule',
      },
      {
        data: { test: 'dogg' },
        path: 'admin', 
        loadChildren: './modules/admin/admin.module#AdminModule' 
      }
    ]
  }

What I'd like to do is pass some data to the PagesComponent component, depending on which lazy loaded module you've loaded.

So, in my PagesComponent I'd like to have something like this:

constructor(private route: ActivatedRoute) {
    this.route.data.subscribe(data => {
      console.log(data);
    });
  }

And the data param will then contain the data defined in the root.

I know I can easily retrieve the data from the routes within the lazy loaded module component itself, but is it possible to get this data in the host component, i.e. PagesComponent?

Background: I'd like to do this so I can display different data in the menu depending on which page you're currently on.

3

1 Answer 1

4

The difference between lazy-loaded and simple routes' data is that you have to look deeper.

constructor(route: ActivatedRoute) {
  route.url.subscribe(() => {
    console.log(route.snapshot.firstChild.firstChild.data);
   });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.