Using redirectTo in a nested <router-outlet>

本秂侑毒 提交于 2019-12-11 16:05:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!