Sub topic 7
The route configuration file
This chapter assumes you know basic TypeScript, can run ng new, and have read how main.ts bootstraps the root component with an application config. It covers app.routes.ts, the file that tells the router which component to show for each URL.
Where the file fits
The CLI creates src/app/app.routes.ts when you answer yes to the routing prompt in ng new. The file exports a routes array. The application config passes that array to provideRouter, and main.ts hands the config to bootstrapApplication.
The routes array
A new workspace starts with an empty array. The type Routes is an alias for Route[], and each Route is an object that matches a URL path to a component.
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { ProductListComponent } from './products/product-list/product-list.component';
export const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'products', component: ProductListComponent },
{ path: '**', redirectTo: '' },
];Each entry uses these properties:
pathis the URL segment without a leading slash. An empty string matches the root URL.componentis the standalone component the router renders.redirectTosends the user to another path. A redirect from the empty path also needspathMatch: 'full'.**is the wildcard. It matches any URL that no earlier entry matched.
The router checks entries from top to bottom and uses the first match. Put the wildcard last, or it will hide every route after it.
Showing the matched component
The route table only decides which component to use. The root component must contain a place to render it. Import RouterOutlet and add <router-outlet /> to the template. Use routerLink to navigate without a full page reload.
import { Component } from '@angular/core';
import { RouterLink, RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet, RouterLink],
template: `
<nav>
<a routerLink="/">Home</a>
<a routerLink="/products">Products</a>
</nav>
<router-outlet />
`,
})
export class AppComponent {}Lazy loading with loadComponent
Importing every page at the top of the file puts all of them in the initial bundle. As the app grows, use loadComponent with a dynamic import(). The CLI then splits that page into its own file and downloads it on first visit.
import { Routes } from '@angular/router';
export const routes: Routes = [
{
path: 'products',
loadComponent: () =>
import('./products/product-list/product-list.component').then(
(m) => m.ProductListComponent,
),
},
];A route uses either component or loadComponent, not both.
Common errors
- Nothing renders. The template has no
<router-outlet />, or the component does not listRouterOutletinimports. - Navigation does not work.
provideRouter(routes)is missing fromapp.config.ts. - Wrong page for a known URL. A wildcard or a broader path appears earlier in the array.
- A redirect loops or fails.
pathMatch: 'full'is missing on an empty-path redirect.
Exercise
You add { path: '**', redirectTo: '' } as the first entry of the routes array. What happens to a visit to /products, and how do you fix it?
Show answer
The wildcard matches every URL, and the router uses the first match. So /products redirects to the root and the products route never runs. Move the wildcard to the end of the array.
Exercise
What does the CLI do differently for a route that uses loadComponent with import() than for one that uses component?
Show answer
It puts the component in a separate JavaScript chunk instead of the initial bundle. The browser downloads that chunk the first time the user visits the route.