angular - 从辅助路由到根的 Angular 6 路由
问题描述
我在顶部有一个主导航栏,其中包含 Home( /
) 和 Information( /information
) 项。在 上/information
,我还有一个带有一堆项目的侧边栏。我正在为这些项目使用辅助路由,以便得到类似/information(aux:item1)
.
每当我路由到我的任何主要导航栏的路由时,我都会Cannot match any routes
在我的 URL 前收到一个带有额外斜杠的错误。因此,如果我单击主页,我希望 URL 是/
,但实际上是//
。
这是一些代码:
应用程序路由.module.ts
const routes: Routes = [
{ path: '', component: HomeComponent, pathMatch: full },
{
path: 'information',
component: InformationComponent,
children: [
{ path: 'item1', component: OneComponent, outlet: 'aux' },
{ path: 'item1', component: TwoComponent, outlet: 'aux' },
...
]
}
];
navbar.component.ts
routes = [
{ name: 'Home', path: '/' },
{ name: 'Information', path: '/information' },
...
];
ngOnInit() {
this.router.events.subscribe((event) => {
if (event instanceof NavigationStart)
console.log(event.url); // Prints '//' when navigated to '/' and '//information' when navigated to '/information'
}
}
navbar.component.html
<a *ngFor="let r of routes" '[routerLink]'="[{ outlets: { primary: route.path, aux: null } }]">{{ route.name }}</a>
信息.component.html
<ul>
<li><a [routerLink]="[{ outlets: { 'aux': 'item1' } }]"></a></li>
<li><a [routerLink]="[{ outlets: { 'aux': 'item2' } }]"></a></li>
</ul>
我想我的问题是,为什么要在我的路线中添加一个额外的斜线?我该如何解决这个问题,以便我可以在辅助路由和主根路由之间来回切换?
解决方案
从 routes.path 中删除 / 它将供参考,请参阅
https://angular-2-training-book.rangle.io/handout/routing/aux-routes.html
推荐阅读
- excel - 从 Excel VBA 中的字母数字字符串中提取十进制数
- javascript - AnimatedProps React Native 0.62 类型上不存在属性“ref”
- c# - 私有静态和私有方法的困境
- python - keras显着性检验中逻辑回归的标准误差
- c++ - 任何人都可以帮助在以下快速排序代码中找到错误,它显示分段错误(SIGSEGV)
- excel - 打开具有特定扩展名的文件
- linux - 有人在 Raspberry pi 3(ARM) 上正确安装了 aws-cli v2(x86_64; zipped) 吗?
- javascript - 如何以角度将字符串解析为特定模式?
- jenkins - 无法在舞台内跳过舞台
- r - R代码逐个打开文件执行操作并关闭打开的文件