1

I have this route:

{ 
  path: ':parent_id',
  component: Parent, 
  children: [
    {
      path: 'child',
      children: [
        {
          path: ':child_id',
          component: Child
         }
      ]
    }
  ]
}

How do I retrieve the parent_id value from within the Child component?

1 Answer 1

3

You should be able to access it using the ActivatedRoute

First import it: import {ActivatedRoute} from "@angular/router";

Then try something like this:

constructor(private route: ActivatedRoute) {

  this.route.parent.params.subscribe((params) => {
    console.log(params); // This would show you all the available params

    if (params['parent_id']) { 
      console.log(params['term']) // Prints the console parent_id param
    }
  });

}
Sign up to request clarification or add additional context in comments.

4 Comments

I have this in ngOnInit: this.route.paramMap.subscribe((p: ParamMap) => { const parent_id = p.get('parent_id'); ... but it is null.
Try using this.route.parent.params instead of this.route.params
nice. I just noticed that ActivatedRoute had a parent property. Thanks. this.route.parent.paramMap.subscribe((p: ParamMap) => { console.log(p.get('parent_id')); });
Nice, I updated my answer to say route.parent.params

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.