Issue
I have the following route configuration in my Angular project:
export const routes: Routes = [
{ path: 'a/:id', loadChildren: './a/a.module#AModule', data: {a: a}},
{ path: 'b/:id', loadChildren: './b/b.module#BModule', data: {a: b}}
];
Now I can get my route configuration like this:
this.router.config //outputs the above array
The this.router.url
shows a
, but how would I correctly identify the correct Route
since the configuration has the unresolved path and the router holds the resolved path.
The reason I want to do that is to access the data
in the app.component
where my router-outlet
is and not in the component itself (the angular documentation says that the data
object is only accessible to the route component itself.
Solution
I solved it using this code:
this.router.events.subscribe(event => {
if (event instanceof RoutesRecognized) {
console.log(event.state.root.firstChild.data['a']);
}
});
Answered By - moritzg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.