Angular2 activate route parameters and parent parameters

How to get RouteParams from parent component as well as child routes

export const routes: Routes = [ { path: '', redirectTo: 'list', pathMatch: 'full' }, { path: 'list', component: UsersComponent }, { path: ':id', component: UserDetailComponent, children: [ // { path: '', redirectTo: 'overview', pathMatch: 'full' }, { path: 'overview', component: UserInfoComponent }, { path: 'specs/:id', component: UserProfileComponent } ] } ]; 

In component

 export class UserDetailComponent implements OnInit { constructor(private route: ActivatedRoute) { } ngOnInit() { this.route.parent.params.subscribe( (c) => { console.log(c['id']); }); this.route.params.subscribe(params => { console.log(params['id']); }); } } 

But I always get undefined when my route is activated. kindly help solve this problem?

Thanks in advance.

+5
source share
2 answers

you need to activate RouteSnapshot to move activated routes.

https://angular.io/docs/ts/latest/api/router/index/ActivatedRouteSnapshot-interface.html

+4
source

Here is a simple example of how to get the value of a parameter in .ts.After that, you can configure it in your case

 import { ActivatedRoute } from '@angular/router'; constructor(private route: ActivatedRoute) { const taskId: string = route.snapshot.params["taskId"]; console.log("this is taskId value = "+ taskId); } 
+1
source

Source: https://habr.com/ru/post/1262564/


All Articles