Sub topic 5
Default Routes, Redirects, and Not-Found Pages
This chapter assumes you can define a Routes array, register it with provideRouter, and render matches in a RouterOutlet. Here you will handle three edge cases that every app has. The first is a visit to the bare root URL, the second is a URL that has moved, and the third is a URL that matches nothing.
How the Router Picks a Route
The router checks routes in the order you list them and uses the first one that matches. This rule explains most of the behavior in this chapter.
Default Route with an Empty-Path Redirect
A redirect route has redirectTo in place of component. The common pattern sends the empty path to a real page:
{ path: '', redirectTo: 'home', pathMatch: 'full' }The pathMatch property controls how much of the URL must match:
| `pathMatch` | Meaning | Effect on `path: ''` |
|---|---|---|
| `'prefix'` (default) | The route path must match the start of the remaining URL | Matches every URL, because every URL starts with an empty string |
| `'full'` | The route path must match the whole remaining URL | Matches only the bare root |
Redirecting Moved URLs
Redirects also keep old links working after you rename a route. Parameters with the same name carry over into the target:
{ path: 'item/:id', redirectTo: '/products/:id' }A visit to /item/42 lands on /products/42. A target that starts with / is absolute and resolves from the root. Without the slash, the target is relative to the current route level.
The Wildcard Route
The path '**' matches any URL. Put it last, because any route listed after it can never match:
import { Routes } from '@angular/router';
import { HomeComponent } from './home.component';
import { ProductDetailComponent } from './product-detail.component';
import { NotFoundComponent } from './not-found.component';
export const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent, title: 'Home' },
{ path: 'item/:id', redirectTo: '/products/:id' },
{ path: 'products/:id', component: ProductDetailComponent },
{ path: '**', component: NotFoundComponent, title: 'Page not found' }
];The not-found component is an ordinary component:
import { Component } from '@angular/core';
import { RouterLink } from '@angular/router';
@Component({
selector: 'app-not-found',
standalone: true,
imports: [RouterLink],
template: `
<h2>Page not found</h2>
<p>The page you asked for does not exist.</p>
<a routerLink="/home">Go to the home page</a>
`
})
export class NotFoundComponent {}Common Errors
- Wildcard placed early. Every route after
'**'is unreachable. - Missing
pathMatch: 'full'on an empty-path redirect, which catches every URL. - Redirect loops. Two redirects that point at each other make navigation fail. Each redirect should end at a route that renders a component.
- Typos in
redirectTo. A misspelled target falls through to the wildcard, so the user sees the not-found page with no error message.
Exercise
A user visits /home, but your app shows the not-found page. The routes are [{ path: '**', component: NotFoundComponent }, { path: 'home', component: HomeComponent }]. What is wrong, and how do you fix it?
Show answer
The router uses the first match, and '**' matches every URL, so the home route is never checked. Move the wildcard route to the end of the array.
Exercise
Why does { path: '', redirectTo: 'home' } without pathMatch break navigation to /products/7?
Show answer
The default pathMatch is 'prefix', and every URL begins with an empty path, so this redirect matches /products/7 too. Add pathMatch: 'full' so it matches only the bare root URL.