在@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! –