Defining the Routes

A route pairs a path with the component to show for it. List them in app.routes.ts, the home page at the empty path and a page-not-found route with ** last, because the router uses the first route that matches. A route can also set the page's title or redirect an old address.

Apprentice · Angular · Updated

After this lesson you can

  • Define an app's routes in app.routes.ts, with a page-not-found route last

Key takeaways

  • A route pairs a path with the component to show for it, and its title sets the browser tab's title.
  • A path has no leading slash: 'categories' matches /categories, and '' is the home page.
  • The router uses the first route that matches, so the '**' page-not-found route always goes last.
  • redirectTo keeps an old address working by sending it on to the new route.

The router needs a list: for each address, which component to show. Each entry in the list is a route.

The pages

Pages are ordinary components. Make one for each page of the app:

ng generate component home
ng generate component categories-page
ng generate component products-page
ng generate component not-found

Each gets its own folder, such as src/app/categories-page/categories-page.ts, with the class CategoriesPage.

The routes

Fill in src/app/app.routes.ts:

import { Routes } from '@angular/router';
import { Home } from './home/home';
import { CategoriesPage } from './categories-page/categories-page';
import { ProductsPage } from './products-page/products-page';
import { NotFound } from './not-found/not-found';

export const routes: Routes = [
  { path: '', component: Home, title: 'Northwind' },
  { path: 'categories', component: CategoriesPage, title: 'Categories' },
  { path: 'products', component: ProductsPage, title: 'Products' },
  { path: '**', component: NotFound, title: 'Page not found' }
];

Each route is an object:

  • path is the address after the host, without a leading slash: 'categories' matches http://localhost:4200/categories. The empty path '' is the home page, http://localhost:4200/.
  • component is the component to show in <router-outlet /> for that path.
  • title sets the browser tab's title while the page is showing.

It's the same idea as routing in ASP.NET Core, where [Route("categories")] sends an address to a controller. Here the address goes to a component, and it all happens in the browser.

Address Shows Tab title
http://localhost:4200/ Home Northwind
http://localhost:4200/categories CategoriesPage Categories
http://localhost:4200/products ProductsPage Products
http://localhost:4200/cheese NotFound Page not found

Page not found, and why it's last

The path '**' matches any address. The router checks the routes in order and uses the first one that matches, so '**' must come last: it catches only the addresses no other route wanted. Put it first, and every address shows NotFound.

Give NotFound a friendly message and a way home:

<h1>Page not found</h1>
<p>Sorry, there's nothing at this address. Try the <a href="/">home page</a>.</p>

(The next lesson replaces that href with a better kind of link.)

Redirects

When a page moves, keep the old address working with redirectTo: the router changes the address and shows the new route's page.

{ path: 'all-categories', redirectTo: 'categories' },

Like the other routes, it goes before '**'.

Try it

Run ng serve, and type each address from the table into the browser. The header and footer from the last lesson stay, the middle changes, and so does the tab's title. Type an address that doesn't exist, and you get your page-not-found page.

Do it: find the page for an address, the way the router does

The router reads the list of routes from the top, and shows the component of the first route that matches the address. Write that search yourself, in plain JavaScript, for the Northwind app's routes.

The function gets two values:

  • routes: the list of routes, in order. Each route has a path ('categories') and a component, here the component's name as text ('CategoriesPage').
  • path: the address after the host, without a leading slash ('products'). The home page is ''.

The tests use the Northwind routes from this lesson:

const routes = [
  { path: '', component: 'Home' },
  { path: 'categories', component: 'CategoriesPage' },
  { path: 'products', component: 'ProductsPage' },
  { path: '**', component: 'NotFound' },
];

For example, the address 'products':

Route checked Does it match 'products'?
'' No
'categories' No
'products' Yes: the search stops here

So pageFor(routes, 'products') returns 'ProductsPage'.

Your job: write the function pageFor(routes, path), which returns the name of the component to show.

  • Check the routes in order, and return the component of the first route that matches.
  • A route matches when its path is exactly the same as path.
  • A route whose path is '**' matches any address.
  • If no route matches, return null.

Practiced live on the lesson page: write the JavaScript right there and check it.

Check yourself

1 question to try on the lesson page.

So what

app.routes.ts is the app's map: a path and a component per page, the home page at '', and '**' last to catch the rest. The router reads it top to bottom, and the first match wins.

Key terms

Cheat sheets