Chapter 7: Routing Basics

Routing Basics — this is where your Angular app stops being a single page and becomes a real multi-page application with navigation, deep linking, and clean URLs.

In modern Angular (v19–21 as of 2026), routing is standalone-first, meaning no NgModule needed for most projects. Everything uses functional APIs (provideRouter, withComponentInputBinding, etc.), and it’s much cleaner and more type-safe than the old RouterModule.forRoot() style.

We’ll go slow and detailed, like always — with complete, runnable examples you can copy-paste into your existing project.

1. What is Angular Routing? (Quick overview)

Routing lets you:

  • Define different views/pages (components) for different URLs
  • Navigate between them without full page reloads (Single Page App magic)
  • Pass data via route parameters (e.g. /user/123)
  • Show content in a placeholder (<router-outlet>)
  • Support lazy loading, guards, resolvers (later chapters)

2. Setting up the Router (Standalone APIs – 2026 style)

When you created your project with:

Bash

Angular already set up routing for you! Look at src/main.ts (or src/app/app.config.ts if using provideIn style):

TypeScript

And src/app/app.routes.ts (this is where all routes live):

TypeScript

That’s it — no more RouterModule.forRoot()!

3. Creating Our First Routes

Let’s build a simple app with 3 pages:

  • Home (/)
  • About (/about)
  • User Detail (/user/:id) – with route parameter

First, generate some components:

Bash

Now update app.routes.ts:

TypeScript

4. Displaying Routes – The RouterOutlet

In app.component.html (your root component), replace everything with:

HTML
  • <router-outlet> is where Angular renders the component matching the current URL
  • routerLink is the Angular way to navigate (instead of <a href> – prevents full reload)

Add some basic content to each component:

home.component.html

HTML

about.component.html

HTML

user-detail.component.html

HTML

5. Route Parameters & ActivatedRoute (Getting :id)

In user-detail.component.ts — we need to read the :id from the URL.

Modern way with inject() + ActivatedRoute:

TypeScript

Now when you click <a routerLink=”/user/123″>:

  • URL becomes /user/123
  • userId() shows 123
  • Change to /user/456 → auto-updates to 456 (thanks to signals!)

Alternative (older but still common) – using snapshot:

TypeScript

But toSignal() + computed() is reactive — perfect if params change without full navigation (e.g., same component reused).

6. Bonus: Active Link Styling & routerLinkActive

Make navigation highlight the current page:

Update app.component.html nav:

HTML

Add CSS in app.component.css:

CSS

→ Current page gets blue background — nice UX!

7. Quick Comparison: Old vs Modern Routing (2026)

Feature Old (NgModule style) Modern (Standalone + functional)
Setup RouterModule.forRoot(routes) provideRouter(routes)
Lazy loading loadChildren: () => import(…).then(m => m.Module) loadComponent: () => import(‘./lazy.component’)
Input binding Manual withComponentInputBinding() (later)
Params access route.snapshot or params.subscribe toSignal(route.params) + computed
Imports needed RouterModule in module Nothing – built-in

8. Mini Practice Task (do this now!)

  1. Add a new route: /contact with a ContactComponent
  2. Add a link in nav: <a routerLink=”/contact” routerLinkActive=”active”>Contact</a>
  3. In UserDetailComponent, show a fake user object:
TypeScript

Template:

HTML
  1. Navigate to /user/123 and /user/999 → see how it handles missing users

Summary of what you learned today

  • Set up router with provideRouter(routes)
  • Define routes with { path, component }
  • Use <router-outlet> to render routed components
  • Navigate with routerLink
  • Read route params with ActivatedRoute + toSignal()
  • Style active links with routerLinkActive

You’ve now got full navigation working — your app feels like a real website!

Next chapter is usually Advanced Routing (lazy loading, child routes, route guards, resolvers) or HTTP & Async Operations (fetching data). Which one would you like to tackle next? Or want to debug/add more features to this routing setup? Just say the word — I’m right here! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *