当我有以下路线
<a mat-list-item [routerLink]="['../../mfe-h/admin/dashboard']"><span>Home</span></a>
异常
Error: NG04005: Invalid number of '../'
at createPositionApplyingDoubleDots (router.mjs:1082:19)
at findStartingPosition (router.mjs:1072:12)
at createTreeUsingPathIndex (router.mjs:907:34)
at createUrlTree (router.mjs:917:20)
at Router.createUrlTree (router.mjs:5245:16)
at get urlTree [as urlTree] (router.mjs:5829:28)
at RouterLinkWithHref.updateTargetUrlAndHref (router.mjs:5821:26)
at RouterLinkWithHref.ngOnChanges (router.mjs:5797:14)
at RouterLinkWithHref.rememberChangeHistoryAndInvokeOnChangesHook (core.mjs:1523:14)
路线
const adminRoutes = [
{
path: 'mfe-h',
component: AdminToolbarComponent,
children: [
{
path: 'admin',
children: [
{
path: 'dashboard',
component: AdminDashboardComponent,
},
],
},
],
},
];
@NgModule({
declarations: [],
imports: [CommonModule, RouterModule.forChild(adminRoutes)],
})
export class AdminRoutingModule {}
我没有找到这方面的官方文档,但调试后发现这个错误的原因是您添加到
/..
或您的 routerLink
功能的额外navigate
。
考虑以下应用程序路由结构:
[
{
path: 'app'
children:[{path: 'child'}]
}
]
现在,当你在
app/child
上时,你可以通过将 app
传递给你的 ./..
来导航到路径 ruoterLink/navigate
。
但是,如果您将传递给许多
/..
,例如./../../..
,您实际上是在尝试导航到比路由树允许的更高的位置。在这种情况下,Angular 将忽略额外的 /..
,但会抛出 NG04005
错误以指示给定路径有问题。