我有一個組件(如下所示),它實現了相當的教科書訂閱路由參數。從同一頁面上的兩個組件訪問路由參數
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();
}
}
然而,路線:http://localhost:3000/tutorial/chapter/0
如果我從瀏覽器中運行ng.probe($0).componentInstance
(在選擇教程CMP),那麼我可以看到章=未定義 - 爲什麼?
此外,如果我使用this.chapter = +params['id'];
,那麼chapter = NaN?
更新:
我有兩個組成部分,一個父(教程)和兒童(章)的組成部分。兩者相同:
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.chapter = params['id'];
});
}
但是,子組件正確顯示chapter
?
但是我想更新... –
我不明白? –
@JohnBaird - 他意思是這個網址是動態的,他需要一個觀察者 – mrgoos