最新のバージョン@angular/router
3.0.0-rc.1
URL /ルートからパラメータを取得する方法が変更されました。Angular 2 new Router:子コンポーネントのルータパラメータを取得する方法は?
thisドキュメントに基づいて、paramsを購読することでそのパラメータを取得できるはずですが、私の場合は動作していないようです。
私が達成したいのは、paramsを親コンポーネントの子ルートから取得することです。
たとえばのは、これが私のルートであるとしましょう:
const routes: Routes = [
{
path: 'parent',
component: ParentComponent,
pathMatch: 'prefix',
children: [
{
path: ':id',
component: ChildComponent
}
]
}
];
私はid
パラメータを取得し、私のparentComponentでそれを使用したいです。 だから私はこのようにしようとしている。このよう
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! –