延迟加载模块上的角子路由未加载

问题描述 投票:2回答:1

不确定我是否做对了所有事情。但问题是:当我从延迟加载的模块导航到组件的某些子路径时,它根本不会加载。它从延迟加载的模块中重新加载home组件。

app-routing.component.ts

const routes: Routes = [
  {path: 'intel', loadChildren: () => import(`./intel/intel.module`).then(m => m.IntelModule)},
  {
    path: 'planet-detector',
    loadChildren: () => import('./planet-detector/planet-detector.module').then((m) => m.PlanetDetectorModule)
  },
  {path: '', redirectTo: 'space', pathMatch: 'full'},
  {path: '**', component: BlackHoleComponent}
];

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

planet-detector-routing.module.ts

const routes: Routes = [
  {path: '', component: DetectorComponent, children: [
      { path: 'first', component: FirstChildComponent},
      { path: 'second', component: SecondChildComponent}
    ]}
];

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

因此,在上面的示例中,当您放置:'http://localhost:4200/planet-detector/first'时,它将加载DetectorComponent组件而不是FirstChildComponent(URL指向'http://localhost:4200/planet-detector/first')。

[我注意到,当我将PlanetDetectorRoutingModule更改为:

const routes: Routes = [
  { path: '', component: DetectorComponent },
  { path: 'first', component: FirstChildComponent },
  { path: 'second', component: SecondChildComponent }
];

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

然后运行正常。还有一个问题。这些孩子路线分离的好处是什么?

angular lazy-loading
1个回答
2
投票

当在children属性中声明路由时,这意味着它们应作为该组件的子级呈现。

因此,要渲染该路线,<router-outlet>中必须有一个DetectorComponent

© www.soinside.com 2019 - 2024. All rights reserved.