首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在嵌套的<router-outlet>中使用redirectTo

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

Stack Overflow用户
提问于 2018-06-22 21:45:11
回答 2查看 1K关注 0票数 0

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

代码语言:javascript
复制
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没有。

EN

回答 2

Stack Overflow用户

发布于 2018-06-22 21:56:39

需要为test-queue添加另一个具有重定向属性的路径。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2018-06-22 22:19:38

您可以尝试将父路由的pathMatch更改为前缀并删除子路由中的./

(Angular documentation for redirecting)

代码语言:javascript
复制
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,解决方案也可以

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50989400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档