Bird
0
0

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📝 Application Q15 of 15
Angular - Performance Optimization
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?
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] }
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes