问题
I have this very simple router config:
export const routes: Routes = [
{
path: '',
component: MiddleComponent,
pathMatch: 'full',
children: [
{
path: '',
redirectTo: './task-queue',
pathMatch: 'full',
},
{
path: 'task-queue',
component: TestComponent,
},
]
},
];
Demo: https://stackblitz.com/edit/angular-a7sxdf?file=app%2Fapp.routing.ts
Where MiddleComponent's template is just <router-outlet></router-outlet>.
I'd expect that after page load I should be redirected to task-queue but I'm not. I can see the MiddleComponent is rendered but TestComponent isn't.
回答1:
Need to add another path with redirect property for 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 }
]
}
];
Demo
回答2:
You can try this changing pathMatch to prefix for the parent route and removing the ./ in the child route
(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,
},
]
},
];
I got that solution from reading https://github.com/angular/angular/issues/14692 (this does not seem to only apply to lazy routes)
Modified stackblitz
But the solution if redirecting straight away to /task-queue from the parent works too
来源:https://stackoverflow.com/questions/50989400/using-redirectto-in-a-nested-router-outlet