[Angular Router] Lazy loading Module with Auxiliary router

Found the way to handle Auxiliary router for lazy loading moudle and erge load module are different. 

 

In Erge loading, it is recommended to create a shell component, inside shell component you need to define the router-outlet for each Auxiliary routes and primary route.

const routes: Routes = [
  ...
  { path: 'speakers', component: SpeakersComponent, children: [
    { path: 'speakersList', component: SpeakersListComponent, outlet: 'list' },
    { path: ':id', component: BioComponent, outlet: 'bio' }
  ] },
];

For example, in the code above, 'SpeakersComponent' is shell component.

<div class="columns">
  <md-card>
    <router-outlet name="list"></router-outlet>
  </md-card>
  <md-card>
    <router-outlet name="bio"></router-outlet>
  </md-card>
</div>

Inside the html, it defines all the auxilliary routes for shell component. 

 


 

But the story changes when you use lazy loading...

For example, we lazy load a feature module inside our root routes configuration:

  {
    path: 'preview',
    loadChildren: 'app/preview/preview.module'
  },

 

And here is the feature module routes:

复制代码
const routes = [
  {
    path: '',
    component: PreviewLeftComponent
  },
  {
    path: ':id',
    component: PokemonDetailComponent
  },
  {
    path: ':id',
    outlet:'aux',
    component: PreviewDetailComponent
  }
];
复制代码

There is one auxiliary route. But here we didn't use any shell component.

 

This is because I found, when using lazy loading, Angular doesn't goes into Shell component html to find auxiliary routes' router-outlet, it goes to root component,

So we have to define the auxiliary route outlet inside root component:

复制代码
<!-- aoo.component.html -->

<md-sidenav-layout>
  <md-sidenav align="start" mode="side" opened>
    <app-sidenav></app-sidenav>
  </md-sidenav>
  <div class="main-content">
    <router-outlet></router-outlet>
  </div>
  <md-sidenav align="end" mode="side" [opened]="curtPath === 'preview'">
    <router-outlet name="aux"></router-outlet>
  </md-sidenav>
</md-sidenav-layout>
复制代码

Github, Talk

posted @   Zhentiw  阅读(515)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2015-12-17 [Javascript] Advanced Reduce: Flatten, Flatmap and ReduceRight
2015-12-17 [Redux] Composition with Objects
2014-12-17 [Javascript] Function scope
2014-12-17 [AngularJS] ui-router: named views
2014-12-17 [AngularJS] ui-router: Abstract States
点击右上角即可分享
微信分享提示