Search code examples
angularangular-router

Children route param unavailable


That's the second time I'm experiencing something curious in Angular router. I'm currently using Angular v4.3.1.

I'm using the same component, for creating and editing objects. That's the reason I want to define two routes for this component, one with an id, the other one without.

I tried to create the following route:

{
    path: 'company/settings/survey',
    component: SurveyComponent,
    children:[
      {path:':id',
      component:SurveyComponent
      }
    ]
}

but with this case, my id param is never set.

I'm retrieving it with:

  this.route.paramMap.subscribe((params) => {
    if (params.has('id')) {
        //Some stuff
    }
  }

I'm currently using a failback:

  {
    path: 'company/settings/survey',
    component: SurveyComponent,
  }, {
    path:'company/settings/survey/:id',
    component: SurveyComponent
  }

but I'd like to understand the reason my first solution isn't working.

Thanks for your help !


Solution

  • try this:

    {
        path: 'company/settings/survey',
        children:[
          {
              path:'',  component:SurveyComponent
          },
          {
              path:':id',  component:SurveyComponent
          },
        ]
    }
    

    then

    constructor(route: ActivatedRouteSnapshot) {}
    this.route.paramMap.get('id');
    .......