Providing the Router

An Angular app is a single page that shows different components for different addresses. The router reads the URL and shows the matching page component in <router-outlet>, without the browser loading a new page. ng new sets it all up for you.

Apprentice · Angular · Updated

After this lesson you can

  • Explain how the Angular router shows the component for the URL in <router-outlet>

Key takeaways

  • An Angular app is a single page: the router reads the address and swaps the component, without the browser loading a new page.
  • <router-outlet /> marks where the page's component goes; everything around it in App, such as a header and footer, stays on every page.
  • provideRouter(routes) in app.config.ts gives the app its router, and ng new sets it up for you.
  • RouterOutlet must be in App's imports, or Angular doesn't know the <router-outlet> element.

So far, each Angular app has been one page. A real site has many: a home page, a list of categories, a page per product. In ASP.NET Core MVC, each address went to the server, and the server sent back a whole new page. An Angular app works differently.

One page, many components

An Angular app is a single-page application (SPA). The browser loads index.html and the app once. After that, when you go to another "page", Angular doesn't ask the server for a new one. It swaps the component in the middle of the page, and changes the address in the browser's address bar, so it still feels like a new page:

Address What Angular shows
http://localhost:4200/ The Home component
http://localhost:4200/categories The CategoriesPage component
http://localhost:4200/products The ProductsPage component

The part of Angular that does this is the router. It watches the address, finds the component for it, and shows that component. The Back and Forward buttons and bookmarks work too, because the address really changes.

It's like a TV: the screen stays the same, and the channel decides what's on it.

What ng new set up

ng new already prepared the router. Look at three files.

src/app/app.config.ts provides the router to the app, with the list of routes:

import { ApplicationConfig, provideBrowserGlobalErrorListeners } from '@angular/core';
import { provideRouter } from '@angular/router';
import { routes } from './app.routes';

export const appConfig: ApplicationConfig = {
  providers: [
    provideBrowserGlobalErrorListeners(),
    provideRouter(routes)
  ]
};

src/app/app.routes.ts holds the routes, which say which component goes with which address. It starts empty; the next lesson fills it:

import { Routes } from '@angular/router';

export const routes: Routes = [];

src/app/app.ts imports RouterOutlet, and its template, app.html, ends with <router-outlet />.

The router outlet

<router-outlet /> marks where the page's component goes. Replace app.html with a page that has a header and a footer around the outlet:

<header class="bg-dark text-white p-3">
  <span class="fs-4">Northwind</span>
</header>

<main class="container py-4">
  <router-outlet />
</main>

<footer class="text-center text-secondary py-3">Northwind Traders</footer>

The header and footer are part of App, so they stay on every page. Only what's inside <router-outlet /> changes: at /categories the router puts CategoriesPage there, and at /products it puts ProductsPage there. It's like @RenderBody() in an MVC layout: the layout stays, and the page fills the gap.

RouterOutlet must be in App's imports, as ng new left it. Without it, Angular doesn't know the <router-outlet> element.

Check yourself

1 question to try on the lesson page.

So what

The router turns one page into many: it reads the address, and shows the matching component in <router-outlet />, while everything around the outlet stays put. ng new set it up; next, you tell it which component goes with which address.

Key terms

Cheat sheets