Thuta Learning
Angular
AdvancedWeb DevelopmentIntermediate

routerLink & router-outlet

AngularLesson 3225 min

What you'll walk away with

  • Explain the core concept and Angular data flow of routerLink & router-outlet
  • Run the example, predict its state and UI, and test edge and error cases
  • Apply a typed safe pattern in a real project and recognize common pitfalls

Explain it simply

routerLink & router-outlet, in plain language

Instead of the <a> tag, use the routerLink directive for navigation.

Problem → Why → Solution

The problem — why this concept matters

Implementing every internal link as click plus router.navigate loses native link semantics and accessibility.

A common mistake

One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Core concept — understand how it works

routerLink delegates SPA navigation to the router, while router-outlet marks where the matched component tree renders. Preserving native anchor semantics supports keyboard use and opening in a new tab. Define active states, query parameters, fragments, and relative navigation according to the route hierarchy.

Mental model
  1. URL or navigation
  2. router config and guard
  3. component view

Explain the core concept and Angular data flow of routerLink & router-outlet

Core syntax or pattern

typescript
<nav>
  <a routerLink="/home">Home</a>
  <a routerLink="/about">About</a>
</nav>

<router-outlet></router-outlet>

// app-routing.module.ts
const routes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'about', component: AboutComponent }
];

routerLink delegates SPA navigation to the router, while router-outlet marks where the matched component tree renders. Preserving native anchor semantics supports keyboard use and opening in a new tab. Define active states, query parameters, fragments, and relative navigation according to the route hierarchy.

Read the code step by step

  1. URL or navigation<nav>

    Instead of the <a> tag, use the routerLink directive for navigation.

  2. router config and guard// app-routing.module.ts

    routerLink delegates SPA navigation to the router, while router-outlet marks where the matched component tree renders. Preserving native anchor semantics supports keyboard use and opening in a new tab. Define active states, query parameters, fragments, and relative navigation according to the route hierarchy.

  3. component view];

    Build sidebar navigation with routerLink and routerLinkActive, testing child routes, query filters, and keyboard focus.

Execution flow
  1. URL or navigation
  2. router config and guard
  3. component view

Success

Explain the core concept and Angular data flow of routerLink & router-outlet

Failure

Implementing every internal link as click plus router.navigate loses native link semantics and accessibility.

Instead of the <a> tag, use the routerLink directive for navigation.

<router-outlet> is a placeholder directive that tells the Angular router where to display the activated component.

typescript
<nav>
  <a routerLink="/home">Home</a>
  <a routerLink="/about">About</a>
</nav>

<router-outlet></router-outlet>

// app-routing.module.ts
const routes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'about', component: AboutComponent }
];
You should see
(Clicking the Home and About links swaps in the matching component's content without refreshing the page)

Real-world usage

How to apply it in a real project

Build sidebar navigation with routerLink and routerLinkActive, testing child routes, query filters, and keyboard focus.

Mini challenge

Build it yourself

Rebuild the routerLink & router-outlet example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

  • Rebuild the example without copying it
  • Test a normal input and an edge case
  • Predict the compiler or output result before running it
typescript
<nav>
  <a routerLink="/home">Home</a>
  <a routerLink="/about">About</a>
</nav>

<router-outlet></router-outlet>

// app-routing.module.ts
const routes: Routes = [
  { path: 'home', component: HomeComponent },
  { path: 'about', component: AboutComponent }
];

Expected: Explain the core concept and Angular data flow of routerLink & router-outlet

Show hint

Build sidebar navigation with routerLink and routerLinkActive, testing child routes, query filters, and keyboard focus.

Quick quiz 1 / 3

What best describes the core idea of routerLink & router-outlet?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

What to remember

  • URL or navigation → router config and guard → component view
  • routerLink delegates SPA navigation to the router, while router-outlet marks where the matched component tree renders. Preserving native anchor semantics supports keyboard use and opening in a new tab. Define active states, query parameters, fragments, and relative navigation according to the route hierarchy.
  • Implementing every internal link as click plus router.navigate loses native link semantics and accessibility.
  • Build sidebar navigation with routerLink and routerLinkActive, testing child routes, query filters, and keyboard focus.

Angular — Routing — Angular

Easy traps

  • Implementing every internal link as click plus router.navigate loses native link semantics and accessibility.
  • One happy-path result does not prove the feature works; empty, loading, error, and cleanup behavior need explicit tests.

Hands-on Exercise

Rebuild the routerLink & router-outlet example without copying it. Add two normal states, one edge case, and one failure state, and write the expected UI and state transition before running it.

You'll know it worked when: (Clicking the Home and About links swaps in the matching component's content without refreshing the page)

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

routerLink & router-outlet | Thuta Learning