3
"product /:id"、 を使用して、この "sample.com/product/45"と同様のリンクからパラメータを取得します。 :ここでは角度2の "/:params"を使用してパラメータを取得する方法
export const routes: Routes = [
{ path: 'product/:id', component: Product }
];
は、コンポーネントクラス
私は(CONSOLE.LOGしようとしたexport class Product implements OnInit, OnDestroy {
id: number;
private sub: any;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.id = +params['id'];
console.log(this.id); //I get undefined here
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
}
)IDです、私未定義
経路パラメータが表示されますか。 – Sajeetharan
はいいいえ、私がリンクを訪問したときに表示され続けます – Saad
デバッグ時にngOnitの内側に来ませんか? – Sajeetharan