我有一个非常简单的路由器配置:
export const routes: Routes = [
{
path: '',
component: MiddleComponent,
pathMatch: 'full',
children: [
{
path: '',
redirectTo: './task-queue',
pathMatch: 'full',
},
{
path: 'task-queue',
component: TestComponent,
},
]
},
];演示:https://stackblitz.com/edit/angular-a7sxdf?file=app%2Fapp.routing.ts
其中MiddleComponent的模板就是<router-outlet></router-outlet>。
我本以为页面加载后会被重定向到task-queue,但事实并非如此。我可以看到MiddleComponent被渲染了,但是TestComponent没有。
发布于 2018-06-22 21:56:39
需要为test-queue添加另一个具有重定向属性的路径。
export const routes: Routes = [
{ path: '', redirectTo: '/task-queue' ,
pathMatch: 'full'},
{ path: '', component: MiddleComponent,
children: [
{ path: '', redirectTo: '/task-queue',pathMatch: 'full' },
{ path: 'task-queue', component: TestComponent }
]
}
];发布于 2018-06-22 22:19:38
您可以尝试将父路由的pathMatch更改为前缀并删除子路由中的./
(Angular documentation for redirecting)
export const routes: Routes = [
{
path: '',
component: MiddleComponent,
pathMatch: 'prefix',
children: [
{
path: '',
redirectTo: 'task-queue',
pathMatch: 'full',
},
{
path: 'task-queue',
component: TestComponent,
},
]
},
];我通过阅读https://github.com/angular/angular/issues/14692得到了这个解决方案(这似乎不仅仅适用于惰性路由)
但是,如果直接从父目录重定向到/task-queue,解决方案也可以
https://stackoverflow.com/questions/50989400
复制相似问题