在嵌套的<router-outlet>中使用redirectTo

时间:2018-06-22 13:45:11

标签: angular angular-routing

我有一个非常简单的路由器配置:

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没有被渲染。

2 个答案:

答案 0 :(得分:0)

需要为测试队列添加具有重定向属性的另一条路径。

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

答案 1 :(得分:0)

您可以尝试将父路由从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得到了解决方案(这似乎并不仅仅适用于惰性路线)

Modified stackblitz

但是从父级直接重定向到/task-queue的解决方案也可以

相关问题