Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get id from a path with child via routing?

Tags:

angular

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 },
                ],
            },
        ],
    },
];
like image 708
dobogi9589 Avatar asked Sep 06 '25 02:09

dobogi9589


1 Answers

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

like image 181
AliF50 Avatar answered Sep 07 '25 21:09

AliF50



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!