October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk6 min

Add Routing in Angular: Routes, Outlets, Links and Guards

A step-by-step guide to adding routing in Angular with standalone configuration, outlets, routerLink, child routes, lazy loading, guards, and common route-order fixes.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To add routing in Angular, define a Routes array that maps URL paths to components, register it with provideRouter(routes), place <router-outlet /> wherever routed views should appear, and use routerLink for navigation so the page changes without a full reload. Angular’s routing guides document this flow for standalone applications, and the steps below follow it.

Which setup this guide follows

The project’s Angular version and architecture change the registration code, so check them before copying anything. This walkthrough uses the standalone configuration that Angular’s current Define routes guide documents, where routes are registered with provideRouter. Angular also publishes version-specific guides; the Angular v20 Define routes page covers the same route-definition topics for that release. If your project still uses NgModules, follow the guide for your version and apply the same route definitions there.

As an Amazon Associate I earn from qualifying purchases.

Add routing in six steps

1. Define the routes

Create a Routes array that maps each path to a component. In Angular CLI projects, the documented conventional location is src/app/app.routes.ts.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { UserComponent } from './user/user.component';
import { NotFoundComponent } from './not-found/not-found.component';

export const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'user/:id', component: UserComponent },
  { path: '**', component: NotFoundComponent },
];

The :id segment is a path parameter, and ** is the wildcard that catches unmatched URLs. Route order is covered in its own section below.

2. Register the router

In the standalone application configuration, add provideRouter(routes) to the providers array. The Router reference describes the Router and its related APIs if you need the terminology.

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

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

3. Render the active view with router-outlet

Import RouterOutlet into the component that should host routed pages, then place <router-outlet /> in its template. The router inserts the component for the active URL at that location. The Show routes with Outlets guide covers rendering in detail.

import { Component } from '@angular/core';
import { RouterLink, RouterOutlet } from '@angular/router';

@Component({
  selector: 'app-root',
  imports: [RouterOutlet, RouterLink],
  template: `
    <nav>
      <a routerLink='/'>Home</a>
      <a routerLink='/user/42'>User 42</a>
    </nav>
    <router-outlet />
  `,
})
export class AppComponent {}

If the navigation bar stays visible while the content below it swaps, the outlet is working as intended. If nothing appears where the page should be, check this step first.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

4. Navigate with routerLink

Apply routerLink to standard anchor elements instead of plain href links for in-app navigation. The Navigate to routes guide explains the declarative approach, and the Router handles the transition in the browser so the application does not reload the page.

5. Add path parameters and child routes

A path such as user/:id exposes the variable segment to the routed component. ActivatedRoute provides route-specific information, including parameters and data.

import { Component, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

export class UserComponent {
  private route = inject(ActivatedRoute);
  userId = this.route.snapshot.paramMap.get('id');
}

A snapshot reads the parameter once, when the component is created. If the same component stays on screen while the parameter changes, read the parameter through the route’s observables instead.

Use children when only a section of the page should change. The parent component must contain its own <router-outlet /> for the child views to render.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
export const routes: Routes = [
  {
    path: 'settings',
    component: SettingsComponent,
    children: [
      { path: 'profile', component: ProfileComponent },
      { path: 'security', component: SecurityComponent },
    ],
  },
];

6. Lazy-load routes when it helps

loadComponent imports a standalone routed component only when its route becomes active. Whether that is worthwhile depends on the trade-off described in the next section.

{
  path: 'reports',
  loadComponent: () => import('./reports/reports.component').then(m => m.ReportsComponent),
},

How Angular chooses a route

Angular uses first-match-wins. It checks routes in the order they are configured and stops at the first match, so the order of the array decides which component renders. The Define routes guide sets out this behavior. Consider this configuration:

Position Route path What it matches Effect of this position
1 users/new Only the literal URL /users/new Reached only because it sits above the parameterized route
2 users/:id Any single segment after /users/, such as /users/42 If moved above users/new, /users/new would be captured with id set to “new”
3 ** Any URL that no earlier route matched Must stay last, or it hides every route below it

The rule is simple: specific paths first, parameterized paths next, and the wildcard last.

Path parameters and query parameters

Path and query parameters carry different kinds of state, and they are configured differently.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Path parameters are part of the route definition, such as user/:id. They identify a resource and must be present for the route to match.
  • Query parameters are separate URL state, such as ?tab=billing. They do not change which route matches.

Eager or lazy loading

Angular’s guidance is to consider eager loading for primary landing pages and lazy loading for other pages, while accounting for nested lazy-loading costs and the application’s performance needs. The Angular v20 Define routes guide covers both loading approaches.

Consideration Eager loading Lazy loading with loadComponent
When the component’s code is fetched With the initial application bundle When its route first becomes active
Initial JavaScript Includes the component Can reduce the initial bundle
Later navigation No code request when the route opens Adds a request when the route opens; later data requests also apply
Angular’s suggested use Primary landing pages Other pages, after checking nested lazy-loading layers

Angular does not give a universal performance threshold for choosing between the two. Measure startup time and the cost of the first visit to a lazy route in your own application before deciding a route’s loading strategy.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Guards control navigation, not security

Angular’s documented guard types are CanActivate, CanActivateChild, CanDeactivate, and CanMatch. Each one can return a boolean, a UrlTree or RedirectCommand, or an Observable or Promise that produces one of those results. The Control route access with guards guide explains each type.

The CanMatch guard behaves differently from the others. When it returns false, the router tries the other routes that match the URL. This makes it useful for feature-dependent alternatives, where two routes share a path and a condition decides which one applies.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Angular’s route guard documentation is explicit about the limit of guards:

“Never rely on client-side guards as the sole source of access control.”

The guide explains that users can modify JavaScript running in the browser, so a guard can hide a page but cannot protect the data behind it. Authorization has to be enforced on the server for every request that returns protected data.

Troubleshooting common routing problems

  • Nothing renders in a routed area. The component that should host the routed view is missing a <router-outlet />, or RouterOutlet is not in its imports.
  • A child route never appears. The parent route has no children configuration, or the parent component lacks a nested outlet.
  • A specific route is never reached. A broader or parameterized route sits above it in the array. Move the specific route higher.
  • Every unknown URL shows an unexpected page. The ** route is placed before other routes. Move it to the end of the array.
  • A protected page is hidden in the browser but its data is still reachable. Only a client-side guard is in place. Add the matching check on the server.

Routing works once the route array, the provider, an outlet in each host component, and correctly ordered paths are all in place. Each piece is small, so when a page fails to render, check them in that order.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.