私は、ルートパラメータにかなりのテキストブックサブスクリプションを実装するコンポーネントを(以下に示すように)持っています。同じページの2つのコンポーネントからのルートパラメータへのアクセス
export class tutorialComponent implements OnInit, OnDestroy {
private sub: Subscription;
public chapter: string;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.chapter = params['id'];
});
}
ngOnDestroy() {
this.sub.unsubscribe();
}
}
ただし、ルート上:私は、ブラウザからng.probe($0).componentInstance
を実行する場合(チュートリアルCMPを選択した)http://localhost:3000/tutorial/chapter/0
、その後、私は章を参照してくださいすることができます=未定義 - なぜですか?
また、this.chapter = +params['id'];
を使用した場合、章= NaN?
更新:
私は2つのコンポーネント、親(チュートリアル)と子(章)コンポーネントを持っています。両方とも同じを持っている:
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.chapter = params['id'];
});
}
しかし、子コンポーネントが正しくchapter
を表示しますか?
私はそれを更新したいと思います... –
私は分かりませんか? –
@JohnBaird - 彼はURLが動的であることを意味し、彼はオブザーバーが必要です – mrgoos