1

Here is my app.routes:

const routes: Routes = [
    {
      path: "login",
      component: LoginComponent
    },
    {
      path: "users",
      component: UsersComponent
    },
    {
      path: "error",
      component: ErreurComponent
    },
    {
      path: "more/complex/path",
      component: FooComponent
    },
    {
      path: "",
      component: HomepageComponent,
    }
]

export const APP_ROUTER_MODULE = RouterModule.forRoot(routes)

Why can't I specify a state or a name for my routes ? If I want to know if wether or not I am on the error page, I then have to check the current route and look for what I have specified in "path" (see also my FooComponent above). I found this a bit silly. Is there anyway to work around this ?

Scipion
  • 11,449
  • 19
  • 74
  • 139

1 Answers1

1
const routes: Routes = [
    {
      path: "login",
      component: LoginComponent,
      data: { name: 'r1'}
    },
    {
      path: "users",
      component: UsersComponent,
      data: { name: 'r2'}
    },
    {
      path: "error",
      component: ErreurComponent,
      data: { name: 'r3'}
    },
    {
      path: "more/complex/path",
      component: FooComponent,
      data: { name: 'r4'}
    },
    {
      path: "",
      component: HomepageComponent,
      data: { name: 'r5'}
    }
]
class SomeComponent {
  constructor(route:ActivatedRoute) {
    route.data.forEach(data => console.log(data['name']));
  }
}

See also How do I pass data in Angular 2 components while using Routing?

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • Could that bit constructor(route:ActivatedRoute) { route.data.forEach(data => console.log(data['name'])); } be used in an external service ? – Scipion Jan 31 '17 at 13:07
  • Not sure what you mean with "external service", but you can use a guard for example https://angular.io/docs/ts/latest/guide/router.html#!#guards – Günter Zöchbauer Jan 31 '17 at 13:09
  • What I meant was: Is it possible to have something common to all my module's components where I can get this data['name'] information. – Scipion Jan 31 '17 at 13:14
  • 1
    What I want may actually be the resolve guard. Thanks ! – Scipion Jan 31 '17 at 13:21