Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a Route parameter in an Angular child component

Tags:

angular

I'm unable to get a routing parameter from a child component in my Angular 6 application

I have the following routes set up in app-routing.module:

{
    path: 'item/:id',
    component: ItemLayoutComponent,
    children: [
        { path: '', redirectTo: 'inbox',  pathMatch: 'full' },
        { path: 'inbox', loadChildren: './feature/inbox/inbox.module#InboxModule' }
    ]
},

And then this in my inbox-routing-module.ts:

{
    path: '',
    component: InboxComponent,
    children: [
        { path: '', redirectTo: 'messages',  pathMatch: 'full' },
        {
            path: 'messages', component: MessagesComponent
        },
    ]
}

This allows me to have a url that looks like this to get to my messages component:

item/5200/inbox/messages

My problem is that in my ItemLayoutComponent I can get the value of the id field (in this case 5200) with the following code:

this.route.snapshot.paramMap.get('id')

However I need the value of this ID in my MessagesComponent. Currently the same code gives me null.

like image 475
Robbie Mills Avatar asked Jul 21 '18 07:07

Robbie Mills


People also ask

Where do you set the route parameter value in Angular?

The first way is through the route snapshot. The route snapshot provides the initial value of the route parameter map (called the paramMap ). You can access the parameters directly without subscribing or adding observable operators. The paramMap provides methods to handle parameter access like get , getAll , and has .


3 Answers

Turns out I needed the following:

this.route.parent.parent.parent.snapshot.paramMap.get('id')

Thanks to @jonrsharpe for pointing me in the right direction

like image 84
Robbie Mills Avatar answered Sep 26 '22 09:09

Robbie Mills


Try like this:

HTML :

{
    path: 'item/:id',
    component: ItemLayoutComponent,
    children: [
        { path: '', redirectTo: 'inbox',  pathMatch: 'full' },
        { path: 'inbox', loadChildren: './feature/inbox/inbox.module#InboxModule' }
    ]
},

TS:

import { Router, ActivatedRoute } from '@angular/router';

id: any;

constructor(
        private router: Router,
        private route: ActivatedRoute,
    ) {
    }

this.route.paramMap.subscribe(param => {
            this.id = param.get('id');
        });
like image 38
Akj Avatar answered Sep 25 '22 09:09

Akj


Problem I faced was, route param in root module path is not exist in feature module's activate snapshot.

From @jonrsharpe, I used to run while loop in bottom-up approach until route param found in parents.

var cgName: string = "";
cgName = next.paramMap.get('cgname');
var snap = next;

while (cgName === null && snap.parent !== null) {
     cgName = snap.parent.paramMap.get('cgname');
     snap = snap.parent;
}

My route definition was like,

Root Module

const appRoutes: Routes = [
  { path: 'default', component: DefaultComponent },
  {
    path: ':cgname',
    canActivate: [PathGuard],
    component: LoggedUserLayoutComponent,
    children: [
      { path: 'shop', loadChildren: () => import('./../ShoppingModule/shopping.module').then(mod => mod.ShoppingModule) },
      { path: 'welcome', component: WelcomeComponent, canActivate: [AuthGuard] },
      { path: '', component: LoginRedirectProxyComponent, pathMatch: 'prefix' }
    ]
  },
  { path: '', redirectTo: '/default', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent }
];

Feature Module

const routes: Routes = [
  { path: 'product', component: ProductDetailComponent, canActivate: [PathGuard] },
  { path: '', component: ShopPageComponent, canActivate: [PathGuard] },
];
like image 27
Gopinath Avatar answered Sep 23 '22 09:09

Gopinath