W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
相對路徑允許你定義相對于當前 URL 段的路徑。下面的例子展示了到另一個組件 second-component
的相對路由。FirstComponent
和 SecondComponent
在樹中處于同一級別,但是,指向 SecondComponent
的鏈接位于 FirstComponent
中,這意味著路由器必須先上升一個級別,然后進入二級目錄才能找到 SecondComponent
。你可以使用 ../
符號來上升一個級別,而不用寫出到 SecondComponent
的完整路徑。
//In the template
<h2>First Component</h2>
<nav>
<ul>
<li><a routerLink="../second-component">Relative Route to second component</a></li>
</ul>
</nav>
<router-outlet></router-outlet>
除了 ../
,還可以使用 ./
或者不帶前導斜杠來指定當前級別。
要指定相對路由,請使用 NavigationExtras
中的 relativeTo
屬性。在組件類中,從 @angular/router
導入 NavigationExtras
。
然后在導航方法中使用 relativeTo
參數(shù)。在鏈接參數(shù)數(shù)組(它包含 items
)之后添加一個對象,把該對象的 relativeTo
屬性設置為當前的 ActivatedRoute
,也就是 this.route
。
//RelativeTo
goToItems() {
this.router.navigate(['items'], { relativeTo: this.route });
}
goToItems()
方法會把目標 URI
解釋為相對于當前路由的,并導航到 items
路由。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: