AI Hints
Home›Angular Tutorial›Route Parameters, Navigating in Code and Redirects and Not Found
Angular · Lesson 78 of 120

Redirects and Not Found

Handle every address.

⏱ 2 min read▶ Video at 1:33:23🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

What if someone opens the bare home address? Or types an address that doesn't exist? Our app should handle every address.

  • The bare address
  • An address that doesn't exist
  • Handle them all

Two special routes

Redirect to sends one path to another. Path match full means only the exact empty path. Two stars match any address at all. Routes are checked in order, so the two stars must come last.

keyvalue
redirectTo: 'books'send it elsewhere
pathMatch: 'full'only an exact match
path: '**'anything else
Orderfirst match wins

A redirect

The empty path now redirects to books. We open the bare address. Run it. The address became slash books, and the list is showing.

app.routes.tstypescript
import { Routes } from '@angular/router';
import { BookList } from './book-list';
import { BookDetail } from './book-detail';
import { NotFound } from './not-found';

export const routes: Routes = [
  { path: '', redirectTo: 'books', pathMatch: 'full' },
  { path: 'books', component: BookList },
  { path: 'books/:id', component: BookDetail },
  { path: '**', component: NotFound },
];
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 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 not-found.ts
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';

@Component({
  selector: 'app-not-found',
  imports: [RouterLink],
  template: `
    <h2>Page not found</h2>
    <p>We couldn't find that page.</p>
    <a routerLink="/books">Back to the books</a>
  `,
})
export class NotFound {}
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
/
Also uses _bar
1

Not found

The not found page explains the problem, and links back to the books. The two star route shows it, for any unknown address. We open slash pizza. Run it. No route matched, so the not found page appears.

not-found.tstypescript
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';

@Component({
  selector: 'app-not-found',
  imports: [RouterLink],
  template: `
    <h2>Page not found</h2>
    <p>We couldn't find that page.</p>
    <a routerLink="/books">Back to the books</a>
  `,
})
export class NotFound {}
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 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.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 app.routes.ts
import { Routes } from '@angular/router';
import { BookList } from './book-list';
import { BookDetail } from './book-detail';
import { NotFound } from './not-found';

export const routes: Routes = [
  { path: '', redirectTo: 'books', pathMatch: 'full' },
  { path: 'books', component: BookList },
  { path: 'books/:id', component: BookDetail },
  { path: '**', component: NotFound },
];
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
/pizza
Also uses _bar
1

Summary

Redirect to sends one path to another, and two stars catch every unknown address. Keep the two stars last. Next, lazy loading.

Key takeaways
  • redirectTo
  • pathMatch: 'full'
  • path: '**'
Next up: Lazy Loading — load pages when needed