AI Hints
Home›Angular Tutorial›What Routing Is, Routes and router-outlet and routerLink
Angular · Lesson 74 of 120

Routes and router-outlet

Your first pages.

⏱ 2 min read▶ Video at 1:28:32🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Let's give our shop two pages. A home page, and an about page.

  • Two pages
  • Home
  • About

A route

A route is an object, with a path, and a component. The path is the part of the address after the slash, without the slash. An empty path is the home page. The routes live in app dot routes dot ts, which the project already has.

keyvalue
path: 'about'the address /about
path: ''the home page
component: Aboutwhat to show
app.routes.tswhere routes live

app.routes.ts

The routes file imports the two page components. The empty path shows home. The about path shows the about page.

app.routes.tstypescript
import { Routes } from '@angular/router';
import { Home } from './home';
import { About } from './about';

export const routes: Routes = [
  { path: '', component: Home },
  { path: 'about', component: About },
];

The router outlet

The root component imports router outlet. The heading stays on every page. And the page itself appears where router outlet is. We open the address slash about. Run it. The heading is still there, and the about page appears below it.

app.tstypescript
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';

@Component({
  selector: 'app-root',
  imports: [RouterOutlet],
  template: `
    <h1>Page Turners</h1>
    <router-outlet />
  `,
})
export class App {}
Also uses home.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-home',
  template: `<h2>Home</h2><p>Welcome to Page Turners.</p>`,
})
export class Home {}
Also uses about.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-about',
  template: `<h2>About us</h2><p>A small bookshop, since 1999.</p>`,
})
export class About {}
Also uses app.routes.ts
import { Routes } from '@angular/router';
import { Home } from './home';
import { About } from './about';

export const routes: Routes = [
  { path: '', component: Home },
  { path: 'about', component: About },
];
Also uses _url
/about
Also uses _bar
1

What stays, what changes

Everything outside the router outlet stays on every page, like a header, or a menu. Only the outlet's content changes, when the address changes.

keyvalue
Outside the outleton every page
Inside the outletthe current page
Address changesthe outlet changes
Header, menustay put

Summary

Routes link paths to components, and the router outlet shows the current page. Next, links between pages.

Key takeaways
  • { path, component }
  • path: ''
  • <router-outlet />
Next up: routerLink — links between pages