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

routerLink

Links between pages.

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

Users shouldn't type addresses. They click links. But a normal link reloads the whole app. Router link doesn't.

  • Users click links
  • href reloads the app
  • routerLink doesn't

Router link, on an a tag, goes to a path, without reloading. Router link active adds a class, while that link's page is showing. Exact true makes the home link active only on the home page, and not on every page.

keyvalue
routerLink="/books"go without reloading
routerLinkActive="active"class while active
{ exact: true }only an exact match
href="..."reloads: avoid

A menu

The menu has three router links. Each one gets the active class, while its page is showing. The home link needs exact true. We click books. Run it. The address is slash books. The book list shows, and the books link is highlighted.

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

@Component({
  selector: 'app-root',
  imports: [RouterOutlet, RouterLink, RouterLinkActive],
  template: `
    <nav>
      <a routerLink="/" routerLinkActive="active"
         [routerLinkActiveOptions]="{ exact: true }">Home</a>
      <a routerLink="/books" routerLinkActive="active">Books</a>
      <a routerLink="/about" routerLinkActive="active">About</a>
    </nav>
    <router-outlet />
  `,
  styles: `a { margin-right: 1em; color: #666; font-weight: normal; text-decoration: none; }
    .active { font-weight: bold; color: crimson; text-decoration: underline; }`,
})
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 book-list.ts
import { Component } from '@angular/core';
import { BOOKS } from './books';

@Component({
  selector: 'app-book-list',
  template: `<h2>Books</h2>
    @for (b of books; track b.id) { <p>{{ b.title }}</p> }`,
})
export class BookList {
  books = BOOKS;
}
Also uses app.routes.ts
import { Routes } from '@angular/router';
import { Home } from './home';
import { BookList } from './book-list';
import { About } from './about';

export const routes: Routes = [
  { path: '', component: Home },
  { path: 'books', component: BookList },
  { path: 'about', component: About },
];
Also uses books.ts
export interface Book {
  id: number;
  title: string;
  author: string;
  price: number;
  stock: number;
}

export const BOOKS: Book[] = [
  { id: 1, title: 'Dune', author: 'Frank Herbert', price: 9.99, stock: 4 },
  { id: 2, title: 'The Hobbit', author: 'J.R.R. Tolkien', price: 8.99, stock: 0 },
  { id: 3, title: 'Circe', author: 'Madeline Miller', price: 10.99, stock: 2 },
  { id: 4, title: 'Emma', author: 'Jane Austen', price: 6.99, stock: 7 },
  { id: 5, title: 'Persuasion', author: 'Jane Austen', price: 7.49, stock: 1 },
];
Also uses actions.json
[{"click": "a[href=\"/books\"]"}]
Also uses _bar
1

Summary

Router link moves between pages without reloading, and router link active highlights the current one. Next, routes with parameters.

Key takeaways
  • routerLink
  • routerLinkActive
  • { exact: true }
Next up: Route Parameters — one route, many books