Bird
Raised Fist0
Angularframework~10 mins

Lazy loading routes and modules in Angular - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to lazy load the AdminModule in Angular routing.

Angular
const routes: Routes = [
  { path: 'admin', loadChildren: () => import('./admin/admin.module').[1] }
];
Drag options to blanks, or click blank then click option'
Aload(m => m.AdminModule)
Bthen(m => m.AdminModule)
Cfinally(m => m.AdminModule)
Dcatch(m => m.AdminModule)
Attempts:
3 left
💡 Hint
Common Mistakes
Using catch or finally instead of then.
Trying to call load which is not a function.
2fill in blank
medium

Complete the code to define a lazy loaded route for the UserModule.

Angular
const routes: Routes = [
  { path: 'user', [1]: () => import('./user/user.module').then(m => m.UserModule) }
];
Drag options to blanks, or click blank then click option'
AloadChildren
Bcomponent
CredirectTo
Dchildren
Attempts:
3 left
💡 Hint
Common Mistakes
Using component instead of loadChildren.
Using children without lazy loading.
3fill in blank
hard

Fix the error in the lazy loading syntax for the DashboardModule.

Angular
const routes: Routes = [
  { path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').[1] }
];
Drag options to blanks, or click blank then click option'
Athen(m => m.Dashboardmodule)
Bthen(m => m.dashboardModule)
CDashboardModule
Dthen(m => m.DashboardModule)
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong casing like dashboardModule or Dashboardmodule.
Omitting then.
4fill in blank
hard

Fill both blanks to create a lazy loaded route with a preloading strategy.

Angular
import { RouterModule, Routes, [1] } from '@angular/router';

const routes: Routes = [
  { path: 'settings', loadChildren: () => import('./settings/settings.module').[2] }
];
Drag options to blanks, or click blank then click option'
APreloadAllModules
Bthen(m => m.SettingsModule)
CNoPreloading
DloadChildren
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing preloading strategy names.
Wrong import names or missing then.
5fill in blank
hard

Fill all three blanks to configure lazy loading with a preloading strategy and a route guard.

Angular
import { RouterModule, Routes, [1] } from '@angular/router';
import { AuthGuard } from './guards/auth.guard';

const routes: Routes = [
  {
    path: 'profile',
    loadChildren: () => import('./profile/profile.module').[2],
    canLoad: [[3]]
  }
];
Drag options to blanks, or click blank then click option'
APreloadAllModules
Bthen(m => m.ProfileModule)
CAuthGuard
DNoPreloading
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to import preloading strategy.
Using wrong guard name or missing canLoad.

Practice

(1/5)
1. What is the main purpose of lazy loading modules in Angular?
easy
A. To preload all modules at application start for faster navigation
B. To load modules only when the user navigates to their routes, improving startup speed
C. To bundle all modules into a single large file
D. To disable routing in the application

Solution

  1. Step 1: Understand lazy loading concept

    Lazy loading means loading parts of the app only when needed, not all at once.
  2. Step 2: Identify benefit in Angular context

    Loading modules on demand speeds up the initial app load and reduces bundle size.
  3. Final Answer:

    To load modules only when the user navigates to their routes, improving startup speed -> Option B
  4. Quick Check:

    Lazy loading = load on demand [OK]
Hint: Lazy loading delays module load until route is visited [OK]
Common Mistakes:
  • Thinking lazy loading loads all modules upfront
  • Confusing lazy loading with preloading
  • Assuming lazy loading disables routing
2. Which of the following is the correct syntax to lazy load a module in Angular routing?
easy
A. { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }
B. { path: 'admin', loadChildren: './admin/admin.module#AdminModule' }
C. { path: 'admin', component: AdminModule }
D. { path: 'admin', loadModule: () => import('./admin/admin.module') }

Solution

  1. Step 1: Recall Angular lazy loading syntax

    Angular uses dynamic import with loadChildren returning a promise resolving to the module.
  2. Step 2: Match syntax to options

    { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) } uses the correct arrow function with dynamic import and then to get the module class.
  3. Final Answer:

    { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) } -> Option A
  4. Quick Check:

    Dynamic import + loadChildren = { path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) } [OK]
Hint: Use arrow function with import().then() for lazy loading [OK]
Common Mistakes:
  • Using deprecated string syntax for loadChildren
  • Confusing component with module in route config
  • Using wrong property name like loadModule
3. Given this route configuration snippet, what happens when the user navigates to '/dashboard'?
{ path: 'dashboard', loadChildren: () => import('./dashboard/dashboard.module').then(m => m.DashboardModule) }
medium
A. The DashboardModule is loaded immediately when the app starts
B. The app throws a runtime error due to incorrect syntax
C. The DashboardModule is never loaded
D. The DashboardModule is loaded only when the user navigates to '/dashboard'

Solution

  1. Step 1: Analyze the route config with loadChildren

    The route uses lazy loading with a dynamic import function for DashboardModule.
  2. Step 2: Understand lazy loading behavior on navigation

    The module loads only when the user visits '/dashboard', not before.
  3. Final Answer:

    The DashboardModule is loaded only when the user navigates to '/dashboard' -> Option D
  4. Quick Check:

    Lazy loading triggers on route visit [OK]
Hint: Lazy loaded modules load on first route access, not app start [OK]
Common Mistakes:
  • Assuming module loads at app start
  • Confusing lazy loading with eager loading
  • Thinking syntax causes runtime error
4. Identify the error in this Angular route configuration for lazy loading:
{ path: 'profile', loadChildren: './profile/profile.module#ProfileModule' }
medium
A. The string syntax for loadChildren is deprecated and causes errors in Angular 17+
B. The path property should be named routePath
C. The module path should not include './' prefix
D. loadChildren should be replaced with loadModule

Solution

  1. Step 1: Check loadChildren syntax

    The string syntax with '#' is deprecated in modern Angular versions (17+).
  2. Step 2: Identify correct syntax

    Modern Angular requires dynamic import with arrow function for lazy loading.
  3. Final Answer:

    The string syntax for loadChildren is deprecated and causes errors in Angular 17+ -> Option A
  4. Quick Check:

    Deprecated string syntax = The string syntax for loadChildren is deprecated and causes errors in Angular 17+ [OK]
Hint: Use dynamic import syntax, not string with # [OK]
Common Mistakes:
  • Using old string syntax for loadChildren
  • Changing path property name incorrectly
  • Replacing loadChildren with non-existent loadModule
5. You want to lazy load a feature module only if the user is authenticated. Which approach correctly combines lazy loading with route guarding in Angular?
hard
A. { path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canActivate: [AuthGuard] }
B. { path: 'settings', component: SettingsComponent, canActivate: [AuthGuard] }
C. { path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canLoad: [AuthGuard] }
D. { path: 'settings', loadChildren: './settings/settings.module#SettingsModule', canLoad: [AuthGuard] }

Solution

  1. Step 1: Understand lazy loading with guards

    To prevent loading a module unless authorized, use canLoad guard with lazy loading.
  2. Step 2: Analyze options for correct guard usage

    { path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canLoad: [AuthGuard] } uses canLoad with dynamic import syntax, which is correct for lazy loaded modules.
  3. Step 3: Explain why others are incorrect

    { path: 'settings', component: SettingsComponent, canActivate: [AuthGuard] } is eager loading with canActivate. { path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canActivate: [AuthGuard] } uses canActivate which guards route activation but not module loading. { path: 'settings', loadChildren: './settings/settings.module#SettingsModule', canLoad: [AuthGuard] } uses deprecated string syntax.
  4. Final Answer:

    { path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canLoad: [AuthGuard] } -> Option C
  5. Quick Check:

    Lazy load + canLoad guard = { path: 'settings', loadChildren: () => import('./settings/settings.module').then(m => m.SettingsModule), canLoad: [AuthGuard] } [OK]
Hint: Use canLoad guard with lazy loaded modules to block loading [OK]
Common Mistakes:
  • Using canActivate instead of canLoad for lazy loading
  • Using deprecated string syntax for loadChildren
  • Applying guards on components instead of modules