1

I have three nested modules in my app. app.module -> leaves.module -> balance.module I wan't to eager load the leaves module and balance module. The app works as expected when I have the router configuration for both leaves.module and balance.module in leaves.routing.module. But I want the route configuration for the balance.module in the balance.routing.module.

app.module

@NgModule({
  declarations: [AppComponent, Page404Component, DashboardComponent],
  imports: [BrowserModule, LeavesModule, AppRoutingModule],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

app.routing.module

const routes: Routes = [
  { path: "dashboard", component: DashboardComponent },
  { path: "", redirectTo: "dashboard", pathMatch: "full" },
  { path: "**", component: Page404Component }
];
@NgModule({
  imports: [RouterModule.forRoot(routes, { enableTracing: false })],
  exports: [RouterModule]
})
export class AppRoutingModule {}

leaves.module

@NgModule({
  declarations: [
    LeavesComponent,
    ApplyComponent,
    Page404leavesComponent
  ],
  imports: [
    CommonModule,
    BalanceModule,
    LeavesRoutingModule,
  ]
})
export class LeavesModule { }

leaves.routing.module

const routes: Routes = [
  {
    path: "leaves",
    component: LeavesComponent,
    children: [
      {
        path: "apply",
        component: ApplyComponent
      },
      //******HOW TO MOVE CHILD ROUTE CONFIG TO CHILD MODULE ?********* */
      {
        path: "balance",
        component: BalanceComponent,
        children: [
          {
            path: "casual",
            component: CasualComponent
          },
          {
            path: "earned",
            component: EarnedComponent
          },
          {
            path: "",
            redirectTo: "casual",
            pathMatch: "full"
          },
          { path: "**", component: Page404balanceComponent }
        ]
      },
      {
        path: "",
        redirectTo: "apply",
        pathMatch: "full"
      },
      { path: "**", component: Page404leavesComponent }
    ]
  }
];

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

balance.module

@NgModule({
  declarations: [
    BalanceComponent,
    CasualComponent,
    EarnedComponent,
    Page404balanceComponent
  ],
  imports: [
    CommonModule,
    BalanceRoutingModule
  ]
})
export class BalanceModule { }

balance.routing.module

const routes: Routes = [];

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

When I move the router config of balance.module in to balance.routing.module like follows, the balance.module doesn't render at all and page404leaves is shown.

const routes: Routes = [{
        path: "balance",
        component: BalanceComponent,
        children: [
          {
            path: "casual",
            component: CasualComponent
          },
          {
            path: "earned",
            component: EarnedComponent
          },
          {
            path: "",
            redirectTo: "casual",
            pathMatch: "full"
          },
          { path: "**", component: Page404balanceComponent }
        ]
      }];

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

What is the right way to do this? I think in a design perspective, child modules should hold its own routes.

See code on Stackblitz.

Really appreciate your help. Thanks.

dahamv
  • 31
  • 1
  • 1
  • 8
  • What's the purpose of separating the child modules if you are not going to lazy load them? – Berk Kurkcuoglu Nov 06 '20 at 15:40
  • 2
    @BerkKurkcuoglu Thanks for the response. The grand child component in the grand child module is the first page of my app. So I believe that module should be eager loaded. As per separation of concerns, I would like to have the child route configuration inside the child module itself. (if Angular allows that) – dahamv Nov 07 '20 at 03:54
  • 1
    https://stackoverflow.com/questions/46626594/angular-switch-from-lazyloading-to-normal-loading – Berk Kurkcuoglu Nov 09 '20 at 08:22

0 Answers0