2

I have this Routing:

{ path: 'item/:id',
  component: parentComponent,
  children: [
    { path: 'edit', component: childComponent }
  ]
}

To navigate to this Location I use this:

this.router.navigate(['/item/' + item.id + '/edit']);

Now my Question is:

How can now my childComponent know the id? What Code do I need to get the id in the child.component.ts? (parentComponent also Needs to know the id, thats why I dont have the Parameter at the end)

LastChar23
  • 449
  • 1
  • 4
  • 15

1 Answers1

3

In the child component you can add the following line.

You need to target the parent route snapshot because the current snapshot is the current route, which is the child component

constructor(private route: ActivatedRoute) {
  this.id = this.route.parent.snapshot.paramMap.get('id')
  console.log('got the id', this.id);
}

Or if you want to subscribe the param in the child component you can use instead

 constructor(private route: ActivatedRoute) {
    this.route.parent.params.subscribe(params => console.log('params id', params.id));
  }