Angular2获取激活的路由参数和父参数

Sat*_*a V 6 angular2-routing angular2-components angular

如何从父组件以及子路由获取RouteParams

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 }
        ]
    }
];
Run Code Online (Sandbox Code Playgroud)

在组件中

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']);
    });
}
}
Run Code Online (Sandbox Code Playgroud)

但是当我的路线被激活时,我总是得到不确定.请帮助解决这个问题?

提前致谢.

Avt*_*ili 7

这是一个简单的示例,如何在.ts中获取参数值,之后您将能够根据您的情况自定义此值

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

  constructor(private route: ActivatedRoute) {
        const taskId: string = route.snapshot.params["taskId"];
        console.log("this is taskId value = "+ taskId);
    }
Run Code Online (Sandbox Code Playgroud)