Skip to main content
Angular Basics
CHAPTER 10 Beginner

Angular Routing and Navigation

Updated: May 18, 2026
5 min read

# CHAPTER 10

Angular Routing and Navigation

1. Chapter Introduction

A Single Page Application needs to display different "pages" (views) without ever reloading the browser. Angular's Router module handles this by listening to URL changes and swapping in the appropriate Component. This chapter covers everything from basic route setup to advanced lazy loading for performance.

2. Learning Objectives

  • Configure the Angular Router with routes.
  • Use routerLink for navigation.
  • Read route parameters with ActivatedRoute.
  • Implement child routes for nested layouts.
  • Enable lazy loading for feature modules.

3. Setting Up Routes

app.routes.ts
12345678910
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { NotFoundComponent } from './not-found/not-found.component';

export const routes: Routes = [
  { path: '', component: HomeComponent },           // Default route
  { path: 'about', component: AboutComponent },
  { path: '**', component: NotFoundComponent }      // Wildcard (404 page)
];
app.config.ts
123456
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';

export const appConfig = {
  providers: [provideRouter(routes)]
};

4. The Router Outlet

The <router-outlet> acts as a placeholder in your main template. Angular injects the active route's component here.
html
12345678
<!-- app.component.html -->
<nav>
  <a routerLink="/">Home</a>
  <a routerLink="/about">About</a>
</nav>

<!-- The active component renders here -->
<router-outlet></router-outlet>
html
1234567
<nav>
  <a routerLink="/" routerLinkActive="active" [routerLinkActiveOptions]="{exact: true}">
    Home
  </a>
  <a routerLink="/products" routerLinkActive="active">Products</a>
  <a routerLink="/about" routerLinkActive="active">About</a>
</nav>

routerLinkActive automatically adds the CSS class active to the link whose route matches the current URL!

6. Route Parameters

typescript
12
// routes
{ path: &#039;product/:id&#039;, component: ProductDetailComponent }
product-detail.component.ts
1234567891011121314
import { Component, OnInit } from &#039;@angular/core&#039;;
import { ActivatedRoute } from &#039;@angular/router&#039;;

@Component({ selector: &#039;app-product-detail&#039;, templateUrl: &#039;./product-detail.component.html&#039; })
export class ProductDetailComponent implements OnInit {
  productId: string = &#039;&#039;;

  constructor(private route: ActivatedRoute) {}

  ngOnInit(): void {
    // Read the ':id' parameter from the URL
    this.productId = this.route.snapshot.paramMap.get(&#039;id&#039;) || &#039;&#039;;
  }
}
html
12
<!-- Navigate to product detail -->
<a [routerLink]="[&#039;/product', product.id]">View Details</a>

7. Programmatic Navigation

typescript
12345678910
import { Router } from &#039;@angular/router&#039;;

export class LoginComponent {
  constructor(private router: Router) {}

  onLoginSuccess(): void {
    // Navigate programmatically after login
    this.router.navigate([&#039;/dashboard&#039;]);
  }
}

8. Lazy Loading

Instead of loading all components upfront, lazy loading fetches feature modules only when the user navigates to them — dramatically improving initial load time.
typescript
12345678
export const routes: Routes = [
  { path: &#039;&#039;, component: HomeComponent },
  {
    path: &#039;admin&#039;,
    // Loads the AdminModule only when user visits /admin
    loadChildren: () => import(&#039;./admin/admin.module&#039;).then(m => m.AdminModule)
  }
];

9. Child Routes

typescript
12345678910
export const routes: Routes = [
  {
    path: &#039;dashboard&#039;,
    component: DashboardComponent,
    children: [
      { path: &#039;overview&#039;, component: OverviewComponent },
      { path: &#039;analytics&#039;, component: AnalyticsComponent }
    ]
  }
];
html
123456
<!-- dashboard.component.html -->
<div class="sidebar">
  <a routerLink="overview">Overview</a>
  <a routerLink="analytics">Analytics</a>
</div>
<router-outlet></router-outlet>  <!-- Child outlet -->

10. Mini Project: Multi-Page Angular App

text
123456
Routes:
  / → HomeComponent
  /products → ProductListComponent
  /product/:id → ProductDetailComponent
  /about → AboutComponent
  /** → NotFoundComponent (404)

11. Common Mistakes

  • Using href instead of routerLink: <a href="/about"> causes a full page reload, defeating the purpose of SPA. Always use routerLink.
  • Missing <router-outlet>: If you configure routes but don't place <router-outlet> in the template, components will never render.

12. MCQs with Answers

Question 1

What Angular element acts as a placeholder where the active route's component is rendered?

Question 2

What directive is used for navigation links instead of the standard href?

Question 3

What wildcard path

Question 4

How do you read a route parameter like :id from the URL?

Question 5

What is Lazy Loading in Angular routing?

Question 6

What directive automatically adds a CSS class when the router link matches the current URL?

Question 7

Which service is used for programmatic navigation (navigating via TypeScript code)?

Question 8

In router.navigate(['/dashboard']), what is the argument?

Question 9

What is the syntax for lazy loading a module in route configuration?

Question 10

What is the path for a default/home route?

13. Interview Questions

  • Q: What is the difference between ActivatedRoute and Router services?
  • Q: Explain how lazy loading improves Angular application performance.

14. Summary

Angular's Router transforms a collection of components into a coherent multi-page application. By using
routerLink for navigation, <router-outlet>` for rendering, and lazy loading for performance, Angular developers can build applications with dozens of views that feel as fast as a desktop application.

15. Next Chapter Recommendation

Every real application collects user data. In Chapter 11: Forms in Angular, we begin exploring Angular's powerful two-pronged forms system — Template-Driven and Reactive Forms.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·