ng2/routing: Possible to access the 'data' property of a route without visiting it?

7 views
Skip to first unread message

Pratik R

unread,
Mar 22, 2017, 2:27:27 AM3/22/17
to Angular and AngularJS discussion
Hi, 

I was wondering if it was possible to access/retrieve the 'data' property of a route (set up when configuring routes) without actually visiting the route?

So for example, say I have a route configuration as such:


export const HeroesRoutes: RouterConfig = [
  { path: 'about',  component: AboutComponent },
  { path: 'heroes',  component: HeroListComponent },
  { path: 'hero/:id', component: HeroListComponent, data: ["authorized"] }
];



now let's say I'm on the 'about' route, but from the AboutComponent, I want to have a method/service available such that I can map

"/hero/5" ----to----> ["authorized"]


Basically I want to map the string representation of an arbitrary route to its associated data property, without having ever visited that route. One way I know to do it is to use the RouterConfig object and traverse the tree myself by deconstruct the string route myself. But is there an easier way?

Thanks!
Reply all
Reply to author
Forward
0 new messages