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 desk3 min

How to Define a Route in Angular

Define Angular routes with a Routes array and register it with the router. Learn path matching, redirects, nested views, guards, titles, and lazy loading.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Angular, a route maps a URL path or pattern to the component that should render, with optional settings for redirects, nested views, titles, guards, and loading. Define route objects in a Routes array, then register that array with Angular Router. For a standalone application, the current setup uses provideRouter(routes) in the application providers.

Define routes and register them

A route commonly pairs a path with a component. This example defines a home page at the root URL and an admin page at /admin:

As an Amazon Associate I earn from qualifying purchases.

import { Routes } from '@angular/router';
import { HomePage } from './home-page';
import { AdminPage } from './admin-page';

export const routes: Routes = [
  { path: '', component: HomePage },
  { path: 'admin', component: AdminPage },
];

For a standalone application, register the configuration with provideRouter(routes) in the application providers. Route arrays are often kept in a file such as app.routes.ts, but the filename and bootstrap arrangement depend on the project. Follow the structure already in use rather than assuming all Angular applications are set up identically. See Angular’s route definition guide and routing overview.

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

Choose paths and order routes carefully

Static and parameterized paths

A static path names fixed segments, such as admin or settings/account. A colon-prefixed segment is a path parameter: user/:id can match URLs such as user/42, letting the same component serve different IDs. Path parameters are part of the URL path; query-string parameters are separate.

First match wins

Angular checks routes in order and uses the first matching route. Put specific patterns before broader ones. For example, if users/new should display a creation page rather than treating new as an ID, define it before users/:id. Put broad matches after more specific ones, and put the wildcard fallback ** last.

export const routes: Routes = [
  { path: 'users/new', component: NewUserPage },
  { path: 'users/:id', component: UserPage },
  { path: '**', component: NotFoundPage },
];

Redirect paths without catching unintended URLs

Use redirectTo to send a URL to another route. Angular’s default path-matching strategy is prefix, which can match a route when its path is only the beginning of the URL. For a redirect that should apply only to the root path, set pathMatch: 'full':

{ path: '', pathMatch: 'full', redirectTo: '/dashboard' }

Without the full match, the empty path is a prefix of every path and can trigger redirects for URLs beyond the root. Angular explains the distinction in its redirecting routes guide; the available route options are also listed in the Route API.

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

Render nested routes inside a parent view

Use a parent route’s children array when related pages share a parent view. Put a <router-outlet> in the parent component’s template; Angular displays the active child route there.

export const routes: Routes = [
  {
    path: 'account',
    component: AccountLayout,
    children: [
      { path: 'profile', component: ProfilePage },
      { path: 'security', component: SecurityPage },
    ],
  },
];
<h1>Account</h1>
<router-outlet></router-outlet>

Add a page title or guard

Route titles

A route can specify a title. When that route activates, Angular updates the document title. Give pages appropriate titles, including for accessibility and orientation when users navigate among browser tabs.

Route guards

Route definitions can use guards such as canActivate and canMatch to control client-side navigation. Angular also supports multiple routes for the same path, with guard logic helping choose a match. Guards are not a substitute for authorization: enforce access rules on trusted server-side systems as well. See Angular’s route guards guide.

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

Choose eager or lazy loading

Use component when the route’s component is included with the route configuration. Use loadComponent to load a component when its route becomes active, or loadChildren to load a route configuration on demand.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Approach What it does Trade-off
Eager (component) Includes the component with the route configuration. Can add work to the initial bundle, but avoids fetching that component only after its route is selected.
Lazy (loadComponent or loadChildren) Loads a component or route configuration when needed. Can reduce initial loading work, but adds later network requests; deeply nested lazy routes may affect performance.

Angular generally recommends eager loading for primary landing pages and lazy loading for other pages, but this is a guideline, not a guaranteed performance result. Measure the application’s build and runtime behavior before deciding how to organize routes. Details are in Angular’s lazy-loaded routes guidance.

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 *

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
Windows Errors? Fix Them Before They SpreadFree repair scan
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.