简体   繁体   English

Angular RouterLink 相对路径在具有空路径的组件中不起作用

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

I have my child path defined as我将我的子路径定义为

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

The whole path looks like this when I am in CartMainComponent http://mystype.com/brand/MyBrandId/cart In the cart I want to navigate to /brand/MyBrandId/cart so I used routerLink="../cart" in html.当我在 CartMainComponent http://mystype.com/brand/MyBrandId/cart时,整个路径看起来像这样 在购物车中我想导航到/brand/MyBrandId/cart所以我使用routerLink="../cart" html。 But this gave me next error但这给了我下一个错误

Error: Cannot match any routes.错误:无法匹配任何路由。 URL Segment: 'brand/Rotiform/cart/catalog' URL 段:“品牌/Rotiform/购物车/目录”

Only when I do routerLink="../../../cart" it works只有当我执行routerLink="../../../cart"时它才有效

This is illogical, even if the empty path is treated as an additional level of relative path.这是不合逻辑的,即使空路径被视为额外级别的相对路径。 Still I expect to use '..' only twice but not three times我仍然希望只使用 '..' 两次但不是三次

Is it possible to fix it so I can relatively go through route without thinking if there are any empty paths?是否可以修复它,以便我可以相对 go 通过路线而不考虑是否有任何空路径?

Yes, the Angular team added a fix for this issue, but you need to enable it manually by adding是的,Angular 团队为此问题添加了修复程序,但您需要通过添加手动启用它

relativeLinkResolution: 'corrected' in root router configuration. relativeLinkResolution: 'corrected' Ie IE

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

But please, note that this 'bugfix' didn't solve the whole bug.但是请注意,这个“错误修复”并没有解决整个错误。 This is why it is not enabled by default.这就是默认情况下不启用它的原因。 This PR describes the problem: https://github.com/angular/angular/issues/26983 Please keep it in mind.这个 PR 描述了这个问题: https://github.com/angular/angular/issues/26983请记住。

RelativeLinkResolution is also discussed in this github thread https://github.com/angular/angular/issues/13011在此 github 线程https://github.com/angular/angular/issues/13011中也讨论了 RelativeLinkResolution

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM