我的孩子路径定义为
{ path: '', component: CartMainComponent, pathMatch: 'full' }
当我在CartMainComponent中时,整个路径看起来像这样http://mystype.com/brand/MyBrandId/cart
在购物车中,我想导航至/brand/MyBrandId/cart
,因此在html中使用了routerLink="../cart"
。但这给了我下一个错误
错误:无法匹配任何路线。网址段:“品牌/网站/购物车/目录”
仅当我执行routerLink="../../../cart"
时有效
即使将空路径视为相对路径的附加级别,这也不合逻辑。我仍然希望只使用'..'两次,但不会使用三次
是否可以修复它,这样我就可以相对地通过路线而无需考虑是否有空路?
是,Angular团队为此问题添加了一个修复程序,但是您需要通过添加]手动启用它。
relativeLinkResolution: 'corrected'
在根路由器配置中。即
@NgModule({
imports: [RouterModule.forRoot(routes, { relativeLinkResolution: 'corrected'})],
exports: [RouterModule]
})
但是请注意,此“错误修复程序”不能解决整个错误。这就是默认情况下未启用它的原因。此PR描述了问题:https://github.com/angular/angular/issues/26983请记住这一点。
RelativeLinkResolution在此github线程中也进行了讨论https://github.com/angular/angular/issues/13011