title: Angular Lazy Loading Submodule
body: <p>I have an application which uses lazy loading in multiple levels. The initial path of '/' is caught in the main app.routes and redirected to a lazy loaded module. That module also has its own submodule-1.routes and is just a container for other feature modules.  In the submodule-1.routes, I am lazy loading the feature module.  From what I can tell it seems that the modules are not being lazy loaded.</p>
  
  <p>I have tried to create the routes in submodule-1 itself, and lazy load the feature modules from there but that hasnt helped.</p>
  
  <p>Below is the folder structure</p>
  
  <pre><code>app
  app.module.ts
  app.routing-module.ts
  -modules
  --login
  ---login.module.ts
  ---login-routing.module.ts
  ---pages
  ----login-user
  -----login-user.module.ts(LoginUserPageModule)
  -----login-user-page.ts(LoginUserPage)
  --signup
  ---signup.module.ts
  ---signup-routing.module.ts
  ----pages
  -----signup-email
  ------signup-email.module.ts(SignUpEmailPageModule)
  ------signup-email-page.ts(SignUpEmailPage)
  </code></pre>
  
  <p>Below is my main routes in app.routes.ts:</p>
  
  <pre><code>import { NgModule } from '@angular/core';
  import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
  
  const routes: Routes = [
    { path: '', redirectTo: 'login', pathMatch: 'full' },
    { path: 'signup-email', loadChildren: './modules/signup/signup.module#SignupModule' },
    { path: 'login', loadChildren: './modules/login/login.module#LoginModule' },
  
  ];
  
  @NgModule({
    imports: [
      RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
    ],
    exports: [RouterModule]
  })
  export class AppRoutingModule { }
  </code></pre>
  
  <p>Signup Module</p>
  
  <pre><code>import { NgModule } from '@angular/core';
  import { CommonModule } from '@angular/common';
  import { RouterModule } from '@angular/router';
  import { SignupRoutingModule } from './signup-routing.module';
  
  
  @NgModule({
    declarations: [],
    imports: [
      CommonModule,
      SignupRoutingModule
    ],
    exports:[]
  })
  export class SignupModule { }
  </code></pre>
  
  <p>SignupRoutes.ts</p>
  
  <pre><code>import { NgModule } from '@angular/core';
  import { CommonModule } from '@angular/common';
  import { Routes, RouterModule } from '@angular/router';
  
  export const SIGNUP_ROUTES: Routes = [
      {
          path: '',
          loadChildren: './pages/signup-email/signup-email.module#SignupEmailPageModule'
      }
  ];
  
  
  @NgModule({
    declarations: [],
    imports: [
      CommonModule,
      RouterModule.forChild(SIGNUP_ROUTES)
    ],
    exports: [RouterModule]
  })
  export class SignupRoutingModule { }
  </code></pre>
  
  <p>SignupEmailPageModule</p>
  
  <pre><code>import { SignupEmailPage } from './signup-email.page';
  
  const routes: Routes = [
    {
      path: '',
      component: SignupEmailPage
    }
  ];
  
  @NgModule({
    imports: [
      CommonModule,
      FormsModule,
      IonicModule
      ,RouterModule.forChild(routes)
    ],
    declarations: [SignupEmailPage]
  })
  export class SignupEmailPageModule {}
  </code></pre>
  
  <p>Login Module</p>
  
  <pre><code>@NgModule({
    declarations: [],
    imports: [
      LoginRoutingModule
    ],
    providers:[
    ]
  })
  export class LoginModule { }
  </code></pre>
  
  <p>LoginRoutes</p>
  
  <pre><code>export const LOGIN_ROUTES: Routes = [
      {
          path: '',
          loadChildren: './pages/login-user/login-user.module#LoginUserPageModule'
      }
  ];
  
  
  @NgModule({
    declarations: [],
    imports: [
      RouterModule.forChild(LOGIN_ROUTES)
    ],
    exports: [RouterModule]
  })
  export class LoginRoutingModule { }
  </code></pre>
  
  <p>LoginUserPageModule</p>
  
  <pre><code>const routes: Routes = [
    {
      path: '',
      component: LoginUserPage
    }
  ];
  
  @NgModule({
    imports: [
      IonicModule,
      RouterModule.forChild(routes),
      SharedModule
    ],
    declarations: [LoginUserPage]
  })
  export class LoginUserPageModule {}
  </code></pre>
  
  <p>Since the default path of '/' is being directed to login which is working, i expect the signup module to be lazy loaded, and visible only once. However, below is the image that i see from Augury on the initial load of the application which makes it seem like both LoginModule and SignupEmailModule are being loaded multiple times and not lazy loaded at all. As a result, I see the LoginUserPage and SignupEmailPage already loaded as in the image below from Augury.</p>
  
  <p><a href="https://i.stack.imgur.com/AVBiL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AVBiL.png" alt="Augury Representation showing the modules/pages already loaded"></a></p>
  
  <p>After removing the the <code>{ preloadingStrategy: PreloadAllModules }</code> from app.routing.ts. I do see that the modules are now loaded lazily, however, when I access each of the lazy loaded modules, I see an extra 'lazy' node in the Augury graph as below. I'm not sure why that is, and if it should be there ?</p>
  
  <p>Also, whenever a lazyloaded route is loaded, the other route goes back to showing as lazy, should it not just stay loaded from that point on ? Why does it go back to showing as 'lazy' in the graph, almost as if it hasnt been loaded.</p>
  
  <p>Augury graph after accessing the default path which is also lazy loaded
  <a href="https://i.stack.imgur.com/1HxT3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1HxT3.png" alt="Image after access the default path which is also lazy loaded"></a></p>
  
  <p>Augury graph after accessing the lazy loaded route of 'signup' page
  <a href="https://i.stack.imgur.com/OfgcU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OfgcU.png" alt="Image after accessing the lazy loaded route of &#39;signup-email&#39;"></a></p>