Day 4 โ Angular Routing, Navigation, Route Params, Guards & Lazy Loading
๐ Day 4 โ Angular Routing, Navigation, Route Params, Guards & Lazy Loading
Zero to Hero โ Hands-on, Blog-Style Tutorial
By the end of Day 4, youโll know:
- โ๏ธ How Angular routing works
- โ๏ธ How to create pages
- โ๏ธ How to navigate between pages
- โ๏ธ How to extract route parameters
- โ๏ธ How to use signals with routing
- โ๏ธ How to add navigation menus
- โ๏ธ How to create a guard (
AuthGuard) - โ๏ธ How to lazy-load standalone components
Letโs build a real multi-page Angular app! ๐
๐ฆ 1. What Is Angular Routing?
Routing allows Angular to show different components based on URL.
Example:
| URL | Screen |
|---|---|
/ | Home page |
/about | About page |
/users/5 | User detail page |
/dashboard | Admin dashboard (maybe protected) |
Routing = SPA navigation without page reloads.
๐ฉ 2. Create Pages (Standalone Components)
Letโs build 3 pages:
ng g component pages/home --standalone
ng g component pages/about --standalone
ng g component pages/user-details --standalone
Now you have:
pages/
โโ home/
โโ about/
โโ user-details/
๐ง 3. Define Routes (In app.routes.ts)
Open: src/app/app.routes.ts
Replace with:
import { Routes } from '@angular/router';
import { HomeComponent } from './pages/home/home.component';
import { AboutComponent } from './pages/about/about.component';
import { UserDetailsComponent } from './pages/user-details/user-details.component';
export const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'users/:id', component: UserDetailsComponent }
];
- โ๏ธ
:idis a route parameter - โ๏ธ
''means โhome/root routeโ
๐ฅ 4. Add Router to AppComponent
Open: app.component.ts
Add router imports:
import { RouterOutlet, RouterLink } from '@angular/router';
@Component({
...
imports: [RouterOutlet, RouterLink],
})
export class AppComponent {}
Update template: app.component.html
<nav>
<a routerLink="">Home</a>
<a routerLink="about">About</a>
<a routerLink="users/1">User #1</a>
</nav>
<hr />
<router-outlet></router-outlet>
- โ๏ธ
routerLink= navigation - โ๏ธ
router-outlet= where pages display
Now try clicking the links!
๐ฉ 5. Extracting Route Parameters (ActivatedRoute)
In user-details.component.ts:
import { Component, inject, signal } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-user-details',
standalone: true,
templateUrl: './user-details.component.html',
})
export class UserDetailsComponent {
route = inject(ActivatedRoute);
userId = signal('');
constructor() {
this.route.paramMap.subscribe(params => {
this.userId.set(params.get('id') ?? '');
});
}
}
Template:
<h2>User Details</h2>
<p>User ID from URL: {{ userId() }}</p>
Now navigate to: /users/99
You will see:
User ID from URL: 99
- โ๏ธ You learned dynamic routing
- โ๏ธ You used signals to store route data
๐ฆ 6. Programmatic Navigation (Router)
In HomeComponent, add a โGo to userโ button.
home.component.ts:
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
@Component({
selector: 'app-home',
standalone: true,
templateUrl: './home.component.html',
})
export class HomeComponent {
router = inject(Router);
goToUser() {
this.router.navigate(['/users/5']);
}
}
home.component.html:
<h2>Welcome to Home</h2>
<button (click)="goToUser()">Go to User 5</button>
- โ๏ธ
router.navigate()for logic-based navigation
๐ฅ 7. Add Active Route Styling (routerLinkActive)
In your nav:
<nav>
<a routerLink="" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }">Home</a>
<a routerLink="about" routerLinkActive="active">About</a>
<a routerLink="users/1" routerLinkActive="active">User #1</a>
</nav>
Add css:
.active {
font-weight: bold;
color: #1976d2;
}
- โ๏ธ Auto-highlighting of current page
- โ๏ธ Common in dashboards
๐ซ 8. Route Guards โ Protecting Routes
Letโs build AuthGuard.
Generate:
ng g guard auth --standalone
Open: auth.guard.ts:
import { CanActivateFn } from '@angular/router';
export const authGuard: CanActivateFn = () => {
const isLoggedIn = false; // change later โ will use real auth
return isLoggedIn ? true : false;
};
Now protect a route:
{
path: 'dashboard',
loadComponent: () =>
import('./pages/dashboard/dashboard.component').then(m => m.DashboardComponent),
canActivate: [authGuard]
}
Try opening /dashboard โ you get blocked โ๏ธ
Later (Day 8), we build real authentication.
๐ง 9. Lazy Loading (Modern Standalone Version)
Lazy loading is critical in large apps.
Letโs lazy load the About page.
Change:
{ path: 'about', component: AboutComponent }
To:
{
path: 'about',
loadComponent: () =>
import('./pages/about/about.component').then(m => m.AboutComponent)
}
- โ๏ธ No NgModule
- โ๏ธ Page loads only when user navigates
- โ๏ธ Angular 17+ best practice
๐ฆ 10. Real-World Example: Nested Routes
Letโs add:
/users/:id/profile/users/:id/settings
Inside routes:
{
path: 'users/:id',
children: [
{ path: 'profile', loadComponent: () => import('./pages/user-profile/user-profile.component').then(m => m.UserProfileComponent) },
{ path: 'settings', loadComponent: () => import('./pages/user-settings/user-settings.component').then(m => m.UserSettingsComponent) }
]
}
This structure mirrors real enterprise apps.
๐ End of Day 4 โ What You Learned
Today you covered A LOT:
- โ๏ธ Routing basics
- โ๏ธ
routerLink&router-outlet - โ๏ธ Route parameters (
:id) - โ๏ธ
ActivatedRoute+ signals - โ๏ธ Programmatic navigation
- โ๏ธ Active route highlighting
- โ๏ธ Guards (
canActivate) - โ๏ธ Lazy loading
- โ๏ธ Nested routes
- โ๏ธ Building multiple pages
You now understand exactly how modern Angular apps are structured.
This is huge. Youโre leveling up fast ๐ช.
๐งช Day 4 Challenge
Create:
๐ A โProductsโ section with:
/productsโProductListComponent/products/:idโProductDetailsComponent- Fetch the product ID
- Add a โBackโ button using programmatic navigation
- Protect
/products/:idusingauthGuard