Angular Routing, Guards & Lazy Loading #3
Routing, Guards, and Lazy Loading β are core Angular interview topics, especially for real-world apps with multiple modules and role-based access.
Hereβs your paper-and-pen-friendly breakdown, ready for your notebook.
π 1. Angular Routing
Routing lets Angular apps behave like SPAs (Single Page Applications) by navigating between components without reloading the page.
β How It Works
- Routes are defined in a Routes array
- Each route maps a path to a component
- You use
<router-outlet>in your HTML to display the routed component
π§± Example: Basic Routing
// app-routing.module.ts
const routes: Routes = [
{ path: 'home', component: HomeComponent },
{ path: 'profile/:id', component: ProfileComponent },
{ path: '**', redirectTo: 'home' }
];
<!-- app.component.html -->
<nav>
<a routerLink="/home">Home</a>
<a [routerLink]="['/profile', 101]">Profile</a>
</nav>
<router-outlet></router-outlet>
π 2. Route Guards
Guards decide whether a route can be activated, loaded, or deactivated.
π Common Guards
| Guard Type | Purpose |
|---|---|
CanActivate |
Control access to a route |
CanActivateChild |
Control access to child routes |
CanDeactivate |
Confirm before navigating away (e.g., unsaved form) |
CanLoad |
Prevent lazy-loaded module from being loaded |
β CanActivate Example:
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService) {}
canActivate(): boolean {
return this.authService.isLoggedIn();
}
}
const routes: Routes = [
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] }
];
π§³ 3. Lazy Loading
Lazy loading loads feature modules only when the user navigates to them, improving performance.
π§ Setup Lazy Loading:
- Create a Feature Module (e.g., AdminModule)
ng g module admin --route admin --module app.module
- Use
loadChildrenin Route
const routes: Routes = [
{ path: 'admin', loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule) }
];
- In Admin Module, define child routes
const adminRoutes: Routes = [
{ path: '', component: AdminHomeComponent },
{ path: 'users', component: AdminUsersComponent }
];
π Comparison: Lazy Loading vs Eager Loading
| Feature | Lazy Loading | Eager Loading |
|---|---|---|
| Load Time | Only on first access | At app startup |
| Performance | Better for large apps | Slower for big apps |
| Setup | Extra config (separate module) | Default |
| Use Case | Dashboards, admin areas | Home, login, common pages |
π¬ Common Interview Questions
- How to prevent unauthenticated users from accessing routes?
π UseCanActivateguard - Difference between
CanActivateandCanLoad?
πCanActivateprotects access;CanLoadprevents loading lazy module - How do you implement Lazy Loading?
π UseloadChildren+ separate feature module + its own routing - Where to use
CanDeactivate?
π On forms where unsaved data may be lost (e.g., editing profile)
β Summary Table
| Concept | Keyword | Example Use |
|---|---|---|
| Routing | routerLink, router-outlet |
Navigation & views |
| Guard | canActivate, canLoad |
Role-based auth |
| Lazy Loading | loadChildren |
Improve performance |
Would you like next:
- π§ͺ A quick test of 5 questions to practice?
- π Notes on
@ViewChild()+ngAfterViewInit()for DOM access?
Let me know β and keep filling those pages! ποΈπ