Video lecture
Watch on YouTube(opens in a new tab)Sub topic 14
Adding Pages with the Router
This chapter assumes you have a project created with ng new, know how main.ts passes appConfig to bootstrapApplication, and can build a standalone component. You will add two pages and move between them without reloading the browser.
What the Router Does
A single-page application loads one HTML document. When the user navigates, the Angular Router reads the URL, finds a matching route definition, and renders that route's component in a placeholder called <router-outlet>. The browser address bar and history still behave normally, but the server only sees the first request.
Creating the Page Components
Generate one component per page:
ng generate component home
ng generate component aboutRecent CLI versions create src/app/home/home.ts with a class named Home. Older versions create home.component.ts with HomeComponent. If yours does that, adjust the import paths below to match.
Defining Routes
Routes are plain objects in an array. Each one maps a path (without a leading slash) to a component.
// src/app/app.routes.ts
import { Routes } from '@angular/router';
import { Home } from './home/home';
import { About } from './about/about';
export const routes: Routes = [
{ path: '', component: Home },
{ path: 'about', component: About },
{ path: '**', redirectTo: '' },
];The router checks routes from top to bottom and uses the first match. The ** wildcard matches anything, so it must come last.
Registering the Router
Routes do nothing until you register them as a provider in the application config:
// src/app/app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)],
};Adding the Outlet and Links
The root component needs a <router-outlet> to mark where pages render. It also needs links that use routerLink in place of href. Both are standalone directives, so you import them into the component.
// src/app/app.ts
import { Component } from '@angular/core';
import { RouterOutlet, RouterLink, RouterLinkActive } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet, RouterLink, RouterLinkActive],
template: `
<nav>
<a routerLink="/" routerLinkActive="active" [routerLinkActiveOptions]="{ exact: true }">Home</a>
<a routerLink="/about" routerLinkActive="active">About</a>
</nav>
<router-outlet />
`,
styles: `.active { font-weight: bold; }`,
})
export class App {}routerLinkActive adds a CSS class while its link's route is active. Without exact: true, the Home link (/) would stay active on every page, because every URL starts with /.
Common Errors
| Symptom | Cause | Fix |
|---|---|---|
| `'router-outlet' is not a known element` | `RouterOutlet` is not imported | Add it to the component's `imports` |
| Clicking a link reloads the whole page | Used `href` | Use `routerLink` |
| Links do nothing and the page stays blank | Routes never registered | Call `provideRouter(routes)` in `app.config.ts` |
| A valid route always redirects | `**` placed before it | Move the wildcard to the end |
| Path never matches | Wrote `path: '/about'` | Remove the leading slash |
Exercise
You add { path: 'contact', component: Contact } after the '**' route. Visiting /contact shows the Home page. Why, and how do you fix it?
Show answer
The router uses the first route that matches. '**' matches /contact first and redirects to '', which renders Home. Move the contact route above the wildcard.
Exercise
Your Home link stays bold when you visit /about. What is missing?
Show answer
The Home link needs [routerLinkActiveOptions]="{ exact: true }". Without it, "/" counts as a prefix of every URL, so the link is treated as active on every page.