角 V2.2.1,路由器 V3.2.1訂閱兒童路線變化,知道父母在選擇兒童路線段?
我和孩子有一個嵌套的路由器,路由器鏈路看起來是這樣的:
/todo/iwgv4ehyav544f7c/zone1
(zone1
是todo/:id
孩子航線)
/todo/iwgv4ehyav544f7c/zone2
(zone2
是todo/:id
孩子航線)
現在請記住,todo/:id
頁面有它自己的路由器插座,可以顯示孩子。
在父母(todo/:id
)我有2個按鈕,Button1和Button2。
我想在兒童路線zone1
處於活動狀態時顯示Button1,並且在zone2
處於活動狀態時顯示Button2。
我已成功地使東西,但我不認爲這是正確的做法:
router.events
.filter(event => event instanceof NavigationEnd)
.map(value => value.url.split("/")) // what I get: ["","todo","iwgv4ehyav544f7c","zone1"]
.map(value => value[3]) // selecting the value at id 3 which is the child param
.subscribe(response => {
this.activatedChildUrl = response; // end value is "zone1"
})
是否有另一種更好的方式來訂閱到路由器,得到孩子PARAMS,所以我可以使用這個值在父級?
編輯:
其他的事情到目前爲止,我已經試過這並不能阻止維克托Savkin書工作:
activatedRoute.url.subscribe(() =>{
activatedRoute.snapshot.firstChild.url[0].path
}); // is firing only once