Sub topic 2
Defining Routes and Registering the Router
This chapter assumes you know TypeScript, standalone components, and Angular's dependency injection. It covers two steps. First you describe your URLs in a Routes array. Then you hand that array to the router with provideRouter.
The Routes array
A route is a plain object that maps a URL path to a component. Routes is the type for an array of these objects. The router reads the array when the app starts. On each navigation it searches the array for a match.
The properties you will use most often are listed below.
| Property | Purpose | Example |
|---|---|---|
| `path` | URL segment to match, written without a leading slash | `'products/:id'` |
| `component` | Component to render when the path matches | `HomeComponent` |
| `title` | Text the router puts in the browser tab | `'Home'` |
| `redirectTo` | Path to send the user to instead | `'home'` |
| `pathMatch` | `'prefix'` (default) or `'full'` | `'full'` |
Here is a small configuration that runs as shown:
// app.routes.ts
import { Routes } from '@angular/router';
import { Component } from '@angular/core';
@Component({
selector: 'app-home',
standalone: true,
template: `<h2>Home</h2>`
})
export class HomeComponent {}
@Component({
selector: 'app-about',
standalone: true,
template: `<h2>About us</h2>`
})
export class AboutComponent {}
export const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent, title: 'Home' },
{ path: 'about', component: AboutComponent, title: 'About' }
];Registering the router
provideRouter is a function that returns the providers the router needs: the Router service, ActivatedRoute, URL handling, and your configuration. You pass it to bootstrapApplication along with any optional features.
// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { Component } from '@angular/core';
import { provideRouter, withComponentInputBinding, RouterOutlet } from '@angular/router';
import { routes } from './app.routes';
@Component({
selector: 'app-root',
standalone: true,
imports: [RouterOutlet],
template: `<router-outlet />`
})
export class AppComponent {}
bootstrapApplication(AppComponent, {
providers: [provideRouter(routes, withComponentInputBinding())]
});Features such as withComponentInputBinding() are opt-in functions that you add after the routes. That one binds route parameters to component inputs, which a later chapter covers. Projects generated by the Angular CLI usually put these providers in app.config.ts. The router behaves the same either way.
Matching rules
The router walks the array from top to bottom and uses the first route that matches. Order therefore matters. Put specific paths before general ones. A catch-all route goes last.
With the default pathMatch: 'prefix', a route matches if its path is the start of the remaining URL, compared segment by segment. Every URL begins with an empty path, so an empty-path redirect needs pathMatch: 'full'. Without it, the redirect would match every URL.
Common errors
- Leading slash in
path. Writingpath: '/home'throws an error at startup. Write'home'. - Forgetting
provideRouter. InjectingRouteror usingRouterLinkthen fails withNullInjectorError: No provider for Router. - Empty redirect without
pathMatch: 'full'. Every navigation gets redirected, which can cause a redirect loop. - Wrong order. If a route listed earlier matches first, a route listed later never renders.
Exercise
A teammate writes [{ path: '', redirectTo: 'home' }, { path: 'home', component: HomeComponent }] and reports that navigating to /about also ends up on Home. Why, and how do you fix it?
Show answer
The redirect uses the default pathMatch: 'prefix'. The empty path is a prefix of every URL, so the redirect catches /about too. Add pathMatch: 'full' so the redirect only applies when the URL is empty.
Exercise
Your app throws NullInjectorError: No provider for Router at startup. What is the most likely cause?
Show answer
provideRouter(routes) is missing from the providers passed to bootstrapApplication, or from the application config file.