Build your first Angular appPart 6 of 6
Add a second page with the router
In part two you opened app.routes.ts and found an empty array. It has been waiting there since. This part fills it: a home page with your counters, an about page, links between them, and a URL in the address bar that changes without the page ever reloading.
By Adrien Colombier
What you'll have at the end
- Two pages, each its own component, at
/and/about - A navigation bar that switches between them without a reload
- Unknown URLs sent back to the home page
- A production build, and an honest look at what lazy loading changes in it
What you need
- Part five finished: three
<app-counter>components rendered with@for ng serverunning in a terminal- About 10 minutes
Step 1
Generate one component per page
To the router, a page is just a component - the same kind you made in part five. Generate two:
$ ng generate component homeCREATE src/app/home/home.css (0 bytes)CREATE src/app/home/home.spec.ts (518 bytes)CREATE src/app/home/home.ts (177 bytes)CREATE src/app/home/home.html (19 bytes) $ ng generate component aboutCREATE src/app/about/about.css (0 bytes)CREATE src/app/about/about.spec.ts (525 bytes)CREATE src/app/about/about.ts (181 bytes)CREATE src/app/about/about.html (20 bytes)The home page takes over the counters from App. Move the list and the @for loop into it, and import Counter there instead - the component that uses a tag is the one that imports it:
import { Component } from '@angular/core';import { Counter } from '../counter/counter'; @Component({ imports: [Counter], selector: 'app-home', styleUrl: './home.css', templateUrl: './home.html',})export class Home { protected readonly counters = ['Coffees', 'Bugs fixed', 'Commits'];}@for (name of counters; track name) { <app-counter [label]="name" />}The about page only needs a template:
<h2>About</h2> <p>Three counters, two pages, one Angular app. Built in six parts.</p>Step 2
Fill the routes array
A route is a pair: a path in the URL, and the component to show for it. Open the file that has been empty since part two:
import { Routes } from '@angular/router';import { About } from './about/about';import { Home } from './home/home'; export const routes: Routes = [ { path: '', component: Home }, { path: 'about', component: About }, { path: '**', redirectTo: '' },];path: ''is the root of the site -http://localhost:4200/.path: 'about'is/about. No leading slash in a route's path.path: '**'matches anything the others did not, andredirectTosends it home. The router tries routes from top to bottom and takes the first match, so the wildcard must be last - put it first and every URL goes home.
You do not have to register this array anywhere: app.config.ts has passed it to provideRouter(routes) since the day the CLI generated it. You read that line in part two.
Step 3
Give the pages somewhere to appear
The router knows which component goes with which URL. It still needs to know where on the page to put it. That is what <router-outlet /> is - the tag you deleted in part three, and whose import you removed in part four. Both come back now, with a small navigation bar:
<h1>Built by me, with {{ title() }}</h1> <nav> <a routerLink="/">Home</a> <a routerLink="/about">About</a></nav> <router-outlet />import { Component, signal } from '@angular/core';import { RouterLink, RouterOutlet } from '@angular/router'; @Component({ imports: [RouterLink, RouterOutlet], selector: 'app-root', styleUrl: './app.css', templateUrl: './app.html',})export class App { protected readonly title = signal('Angular');}App is now a frame: the heading and the links stay put, and whatever the URL points at is swapped in below them. Click About - the address bar changes to /about, the counters are replaced by the about text, and the terminal stays quiet because nothing was reloaded. Use the browser's back button and you are home again.
Step 4
Refresh on a sub-page
Go to /about and hit refresh. It still works - but it is worth knowing why. There is no about.html file anywhere. The development server answers every path with your index.html, and the router reads the URL once the app has started:
$ curl -s -o /dev/null -w '%{http_code}\n' http://localhost:4200/about200$ curl -s http://localhost:4200/about | grep app-root <app-root></app-root>This is the rule from the end of part three, now with a page that needs it. ng serve does it for you. A real host will not, unless you configure it to send unknown URLs to index.html - skip that, and refresh on /about gives your visitors the host's 404 page instead of yours.
Step 5
Build it, and try lazy loading
Build it the way you did at the end of part three:
$ ng build❯ Building...✔ Building...Initial chunk files | Names | Raw size | Estimated transfer sizemain-N75XW53M.js | main | 213.97 kB | 58.70 kBstyles-5INURTSO.css | styles | 0 bytes | 0 bytes | Initial total | 213.97 kB | 58.70 kB Application bundle generation complete. [2.146 seconds] - 2026-11-02T14:53:01.807Z Output location: /home/you/my-first-angular-app/dist/my-first-angular-appOne main file holds everything, including the about page, even for a visitor who never opens it. The router can instead load a page only when someone navigates to it. Swap component for loadComponent on the about route:
import { Routes } from '@angular/router';import { Home } from './home/home'; export const routes: Routes = [ { path: '', component: Home }, { path: 'about', loadComponent: () => import('./about/about').then((m) => m.About), }, { path: '**', redirectTo: '' },];$ ng build❯ Building...✔ Building...Initial chunk files | Names | Raw size | Estimated transfer sizechunk-EVLMILDN.js | - | 120.03 kB | 36.23 kBmain-EJM3NKJF.js | main | 95.22 kB | 24.18 kBstyles-5INURTSO.css | styles | 0 bytes | 0 bytes | Initial total | 215.25 kB | 60.41 kB Lazy chunk files | Names | Raw size | Estimated transfer sizechunk-IQCQMIVO.js | about | 375 bytes | 375 bytes Application bundle generation complete. [1.317 seconds] - 2026-11-02T14:53:03.763Z Output location: /home/you/my-first-angular-app/dist/my-first-angular-appA new table: Lazy chunk files. The about page is now its own 375-byte file, downloaded the first time someone clicks About. Note the import at the top of the file is gone too - a static import would pull About back into main.
Now look at the initial total. It went up, from 58.70 kB to 60.41 kB transferred. Splitting the code costs a little in itself, and a 375-byte page saves less than that. Lazy loading pays off when a page brings something heavy with it - a chart library, an editor, an admin area most visitors never open. For a page like this one, component is the better choice, and it is fine to change it back.
That is the series. Six parts ago there was nothing on your machine; now there is an Angular app with state in signals, a reusable component with inputs, a list, two pages and a production build - and you have read every error message on the way rather than copying around them.
Everything you build next uses these same pieces. When you want to go further, the official routing guide and components guide pick up exactly where this series stops.