Route Parameters
One route, many books.
Every book needs its own page. But we don't want five routes, for five books. One route, with a parameter, handles them all.
- A page per book
- Not five routes
- One route, a parameter
A parameter in the path
A colon in the path marks a parameter. So books slash colon id matches books slash one, books slash two, and so on. Our app config already turns parameters into inputs. So the component simply declares an input, called id.
| key | value |
|---|---|
| path: 'books/:id' | a parameter |
| /books/3 | id is '3' |
| id = input.required<string>() | read it as an input |
| Always | a string |
app.routes.ts
Books shows the list. And books slash colon id shows one book, whatever the id is.
import { Routes } from '@angular/router';
import { BookList } from './book-list';
import { BookDetail } from './book-detail';
export const routes: Routes = [
{ path: 'books', component: BookList },
{ path: 'books/:id', component: BookDetail },
];The book page
Id is an input, filled in by the router. Book is computed, by finding the book with that id. The id is text, so we turn it into a number. If no book matches, the else block says so. We open books slash three. Run it. The page shows Circe, the book with id three.
import { Component, computed, input } from '@angular/core';
import { BOOKS } from './books';
@Component({
selector: 'app-book-detail',
template: `
@if (book(); as b) {
<h2>{{ b.title }}</h2>
<p>by {{ b.author }} · £{{ b.price }}</p>
<p>{{ b.stock }} in stock</p>
} @else { <p>No book with id {{ id() }}.</p> }
`,
})
export class BookDetail {
id = input.required<string>();
book = computed(() => BOOKS.find(b => b.id === Number(this.id())));
}Also uses book-list.ts
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { BOOKS } from './books';
@Component({
selector: 'app-book-list',
imports: [RouterLink],
template: `
<h2>Books</h2>
@for (b of books; track b.id) {
<p><a [routerLink]="['/books', b.id]">{{ b.title }}</a></p>
}
`,
})
export class BookList {
books = BOOKS;
}Also uses app.routes.ts
import { Routes } from '@angular/router';
import { BookList } from './book-list';
import { BookDetail } from './book-detail';
export const routes: Routes = [
{ path: 'books', component: BookList },
{ path: 'books/:id', component: BookDetail },
];Also uses app.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
template: `<h1>Page Turners</h1><router-outlet />`,
})
export class App {}Also uses books.ts
export interface Book {
id: number;
title: string;
author: string;
price: number;
stock: number;
}
export const BOOKS: Book[] = [
{ id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
{ id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
{ id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
{ id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
{ id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];Also uses _url
/books/3Also uses _bar
1Links with parameters
Router link can take an array. The parts are joined into a path, like books slash four. We open the list, and click Emma. Run it. The address is books slash four, and Emma's page is showing.
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
import { BOOKS } from './books';
@Component({
selector: 'app-book-list',
imports: [RouterLink],
template: `
<h2>Books</h2>
@for (b of books; track b.id) {
<p><a [routerLink]="['/books', b.id]">{{ b.title }}</a></p>
}
`,
})
export class BookList {
books = BOOKS;
}Also uses book-detail.ts
import { Component, computed, input } from '@angular/core';
import { BOOKS } from './books';
@Component({
selector: 'app-book-detail',
template: `
@if (book(); as b) {
<h2>{{ b.title }}</h2>
<p>by {{ b.author }} · £{{ b.price }}</p>
<p>{{ b.stock }} in stock</p>
} @else { <p>No book with id {{ id() }}.</p> }
`,
})
export class BookDetail {
id = input.required<string>();
book = computed(() => BOOKS.find(b => b.id === Number(this.id())));
}Also uses app.routes.ts
import { Routes } from '@angular/router';
import { BookList } from './book-list';
import { BookDetail } from './book-detail';
export const routes: Routes = [
{ path: 'books', component: BookList },
{ path: 'books/:id', component: BookDetail },
];Also uses app.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
template: `<h1>Page Turners</h1><router-outlet />`,
})
export class App {}Also uses books.ts
export interface Book {
id: number;
title: string;
author: string;
price: number;
stock: number;
}
export const BOOKS: Book[] = [
{ id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
{ id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
{ id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
{ id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
{ id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];Also uses actions.json
[{"click": "p:nth-of-type(4) a"}]Also uses _url
/booksAlso uses _bar
1Summary
A colon marks a route parameter, and the component reads it as an input. Router link arrays build the path. Next, navigating from code.
Key takeaways- books/:id
- input.required<string>()
- ['/books', b.id]