3

My Problem is that I can't get some parameters from a url path with a child.
So in my Code I have a parent component with some stuff and a nav-mat-tab followed by this tutorial. It works fine but if I'm on a tab url I can't get the id of the url.

Example:
http://localhost:4200/parent/1 --> id = 1
http://localhost:4200/parent/1/child1 --> id = null
http://localhost:4200/parent/1/child3/2 --> id = null, t_id = null

this.route.paramMap.subscribe(
      params => console.log(params.get('id'))
);
export const appRoutes: Routes = [
    {
        path: 'parent/:id', component: ParentComponent,
        children: [
            { path: 'tab1', component: Tab1Component },
            { path: 'tab2', component: Tab2Component },
            {
                path: 'tab3', component: Tab3Component,
                children: [
                    { path: ':t_id', component: Tab3ChildComponent },
                ],
            },
        ],
    },
];
dobogi9589
  • 31
  • 1
  • Does this answer your question? [Angular : Get value of parameter in parent route](https://stackoverflow.com/questions/55411353/angular-get-value-of-parameter-in-parent-route) – Andrew Allen Sep 30 '22 at 09:36

2 Answers2

1

Try:

this.route.parent.params.subscribe(params => console.log(params));

I got the answer from the end of this page: https://codecraft.tv/courses/angular/routing/nested-routes/#_parent_route_parameters

AliF50
  • 16,947
  • 1
  • 21
  • 37
0

Instead of doing this.route you can do this.route.parent and you will be able to get the id. For example in your case:

this.route.parent.snapshot.paramMap.get('Id') //to get direct id
or
this.route.parent.params.subscribe(params => console.log(params)); //to subscribe to params