【发布时间】:2017-01-10 09:10:59
【问题描述】:
如何从父组件和子路由中获取 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 }
]
}
];
在组件中
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']);
});
}
}
但是当我的路线被激活时,我总是不确定。 请帮忙解决这个问题?
提前致谢。
【问题讨论】:
-
您找到解决方案了吗?我也有同样的问题。
-
尚未寻找解决方案
-
对我来说,我意识到我在
router-outlet之外的组件中调用了ActivatedRoute。这就是我收到undefined的原因。将组件移动到内部router-outlet解决了我的问题。
标签: angular angular2-routing angular2-components