首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套路由角

嵌套路由角
EN

Stack Overflow用户
提问于 2021-01-28 17:45:46
回答 1查看 40关注 0票数 1

我试图在我的myfile.routing.module.ts文件中有两个层次的嵌套路由,但是当我尝试访问该路由时,它会重定向到家乡。这是我的代码:

routing.module.ts

代码语言:javascript
复制
.
.
.
const routes: Routes = [
  {
    path: '',
    children: [
      { path: '', redirectTo: 'home', pathMatch: 'full' },
      {
        path: 'home',
        component: HomeComponent,
        children: [
          { path: '', redirectTo: 'page' },
          {
            path: 'page',
            component: PageComponent,
            canActivate: [PageGuard],
          },
          {
            path: 'more', component: MoreComponent,
            children: [
              { path: '', component: MoreComponent },
              { path: 'plants', component: PlantsComponent },
              { path: 'cars', component: CarsComponent },
              { path: 'pets', component: PetsComponent },
          ]},
        ],
      },
    ],
  },
];

@NgModule({
  imports: [RouterModule.forChild(routes)],
  exports: [RouterModule],
})
export class myFileRoutingModule {}

more.component.html中,我添加了<router-outlet></router-outlet>

home.component.html中,我有这样的链接:

代码语言:javascript
复制
<a [routerLink]="['home/page/more/plants']">plants</a><br />
<a [routerLink]="['home/page/more/cars']">cars</a><br />
<a [routerLink]="['home/page/more/pets']">pets</a><br />

因此,我遇到的问题是,我无法访问每个部分(例如,home/page/more/plants),因为它总是重定向到家乡。

有人知道问题出在哪里吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-28 17:56:08

你不需要第一个空级别,

这可能解决您的问题:

代码语言:javascript
复制
const routes: Routes = [
    { path: '', redirectTo: 'home', pathMatch: 'full' },
    {
      path: 'home',
      component: HomeComponent,
      children: [
        { path: '', redirectTo: 'page' },
        {
          path: 'page',
          component: PageComponent,
          canActivate: [PageGuard],
        },
        {
          path: 'more', component: MoreComponent,
          children: [
            { path: '', component: MoreComponent },
            { path: 'plants', component: PlantsComponent },
            { path: 'cars', component: CarsComponent },
            { path: 'pets', component: PetsComponent },
        ]},
      ],
    }
];

注意,所有随子组件附带的组件都必须包含<router-outlet></router-outlet>,如HomeComponent & MoreComponent

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

https://stackoverflow.com/questions/65942513

复制
相关文章

相似问题

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