在@angular/router
3.0.0-rc.1
的最新版本中从URL /路由获取参数的方式发生了变化。Angular 2 new Router:如何获取子组件的路由器参数?
根据this文档,您应该能够通过订阅参数来获取参数,但在我的情况下似乎不起作用。
我想要实现的是将params引入父组件FROM子路由。
例如,让我们说,这是我的路线:
const routes: Routes = [
{
path: 'parent',
component: ParentComponent,
pathMatch: 'prefix',
children: [
{
path: ':id',
component: ChildComponent
}
]
}
];
我想要得到的id
参数和我为父级使用它。 所以我想是这样的:
export class ParentComponent implements OnInit {
sub: any;
constructor(
private route: ActivatedRoute) {
this.route = route;
}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
let id = params['id'];
console.log(id);
});
}
}
像这样我得到:
未定义
缺少什么我在这里?
谢谢@Brandon! –