【问题标题】:Angular Routing using navigation - children and loadChildren cannot be used together使用导航的角度路由 - children 和 loadChildren 不能一起使用
【发布时间】:2021-10-20 22:28:09
【问题描述】:

我的主组件模板如下所示:

<app-navigation></app-navigation>

<div class="container-fluid">
  <router-outlet></router-outlet>
</div>

这是导航:

   <div >
  <a routerLink="/home" routerLinkActive="active" >Home</a>
  <a  routerLink="/doctors" routerLinkActive="active" >
    Doctors
  </a>
  <a  routerLink="/health" routerLinkActive="active" >
    Health
  </a>
</div>

app.routing.ts

export const appRoutes: Routes = [

  {
    path: 'doctors',
    loadChildren: () => import('./doctore/doctore.module').then(m => m.DoctoreModule)
  },
  {
    path: 'health',
    loadChildren: () => import('./health/health.module').then(m => m.HealthModule),
  },
  {
    path: '**',
    component: NotFoundPageComponent
  }
];

Doctors 组件有一个医生列表,在选择其中一个时, 应用应该路由到子组件DoctorDetailsComponent,路径为doctors/{id}

     <mat-cell [routerLink]="['/doctors', row.id]" routerLinkActive="active">
            {{row.description}} </mat-cell>

如果我将路由设置为这样:

  {
    path: 'doctors',
    loadChildren: () => import('./docspace/docspace.module').then(m => m.DocspaceModule),
    children: [{path: ':id', component: DoctorsDetailsComponent}]   
  },

我收到一个错误:

路由“医生”配置无效:children 和 loadChildren 不能一起使用

【问题讨论】:

标签: angular routes


【解决方案1】:

如果DoctorsDetailsComponentDocspaceModule 内 那么

{
    path: 'doctors',
    loadChildren: () => import('./docspace/docspace.module').then(m => m.DocspaceModule),   
  },

DocspaceRoutingModule

 const routes: Routes = [
   {
     path: '',
     component: DoctorSpaceComponent,
     children: [
       { path: '', redirectTo: ':id', pathMatch: 'full' } 
       { path: ':id', component: DoctorsDetailsComponent }
     ]
   }
 ];

DoctorSpaceComponent.html

    <router-outlet></router-outlet>

【讨论】:

    【解决方案2】:

    我在具体的模块路由ts中处理了。

    【讨论】:

      猜你喜欢
      • 2021-07-02
      • 1970-01-01
      • 2020-02-25
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-17
      • 2021-07-01
      相关资源
      最近更新 更多