簡體   English   中英

Angular RouterLink 相對路徑在具有空路徑的組件中不起作用

[英]Angular RouterLink relative path does not work in component having empty path

我將我的子路徑定義為

{ path: '', component: CartMainComponent, pathMatch: 'full' }

當我在 CartMainComponent http://mystype.com/brand/MyBrandId/cart時,整個路徑看起來像這樣 在購物車中我想導航到/brand/MyBrandId/cart所以我使用routerLink="../cart" html。 但這給了我下一個錯誤

錯誤:無法匹配任何路由。 URL 段:“品牌/Rotiform/購物車/目錄”

只有當我執行routerLink="../../../cart"時它才有效

這是不合邏輯的,即使空路徑被視為額外級別的相對路徑。 我仍然希望只使用 '..' 兩次但不是三次

是否可以修復它,以便我可以相對 go 通過路線而不考慮是否有任何空路徑?

是的,Angular 團隊為此問題添加了修復程序,但您需要通過添加手動啟用它

relativeLinkResolution: 'corrected' IE

@NgModule({ imports: [RouterModule.forRoot(routes, { relativeLinkResolution: 'corrected'})], exports: [RouterModule] })

但是請注意,這個“錯誤修復”並沒有解決整個錯誤。 這就是默認情況下不啟用它的原因。 這個 PR 描述了這個問題: https://github.com/angular/angular/issues/26983請記住。

在此 github 線程https://github.com/angular/angular/issues/13011中也討論了 RelativeLinkResolution

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM