Sub topic 2
Routes as configuration: a quick refresher
This chapter assumes you can build a standalone Angular component and have used the Angular Router at a basic level. It reviews only the routing ideas that lazy loading builds on. Later chapters change how a route finds its code. Here, the route itself stays the same.
A route is a plain object
The router has no special syntax for routes. A route is a TypeScript object in an array of type Routes. Each object describes one rule: when the URL looks like this, do that.
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
export const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
];The path property is the URL segment to match, without a leading slash. An empty string matches the root. The component property names the class to render. Because component is a direct reference, the import statement at the top of the file runs when the file loads. That import is what puts HomeComponent and AboutComponent in the main bundle.
The rule that follows is central to this tutorial. A route object that refers to code through a static import forces that code into the initial download. Lazy loading replaces the direct reference with a function that fetches the code later.
Registering routes
In a standalone application you hand the array to provideRouter in the application config.
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';
export const appConfig: ApplicationConfig = {
providers: [provideRouter(routes)],
};The root component then needs a place to render the matched component. That place is <router-outlet />.
import { Component } from '@angular/core';
import { RouterOutlet, RouterLink } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet, RouterLink],
template: `
<nav>
<a routerLink="/">Home</a>
<a routerLink="/about">About</a>
</nav>
<router-outlet />
`,
})
export class AppComponent {}Later options such as withPreloading(...) are extra arguments to provideRouter. Keep that call in mind, because it is where router behavior is configured.
Nesting with children
A route can hold a children array. The child routes render inside a <router-outlet> in the parent's template, and their paths are appended to the parent's path.
import { Routes } from '@angular/router';
import { ShopComponent } from './shop/shop.component';
import { ProductListComponent } from './shop/product-list.component';
import { ProductDetailComponent } from './shop/product-detail.component';
export const routes: Routes = [
{
path: 'shop',
component: ShopComponent,
children: [
{ path: '', component: ProductListComponent },
{ path: ':id', component: ProductDetailComponent },
],
},
];The URL /shop renders ShopComponent with ProductListComponent in its outlet. The URL /shop/42 renders ProductDetailComponent instead, and :id captures 42 as a route parameter.
A group of child routes is a self-contained unit with its own path prefix. That is why a whole feature area can later become one lazy chunk: the parent only needs to know the prefix.
How matching works
The router walks the array from top to bottom and uses the first route that matches. Order therefore matters.
Two details cause most routing mistakes:
- A wildcard route,
path: '**', matches any URL. Put it last, or it will hide every route below it. - A redirect from the empty path needs
pathMatch: 'full'. Without it, the empty path counts as a prefix of every URL, and the redirect fires for all of them.
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { NotFoundComponent } from './not-found.component';
export const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: '**', component: NotFoundComponent },
];What to carry forward
The chapters that follow only change how a route obtains its code. The matching rules above stay the same.
| Concept | Role in this tutorial |
|---|---|
| `path` | Still decides when the route matches |
| `component` | Static reference, which puts code in the main bundle |
| `children` | A group of routes under one prefix, which can become one lazy chunk |
| `provideRouter` | The place where router features such as preloading are added |
| Route order | The first match wins, so a lazy route can shadow another |
Exercise
A route array lists { path: '**', component: NotFoundComponent } first, followed by { path: 'about', component: AboutComponent }. What happens when a user visits /about, and why?
Show answer
The user sees NotFoundComponent. The router uses the first matching route, and ** matches every URL, so the about route is never reached. The fix is to move the wildcard route to the end of the array.