← Back to Chapters

Standalone Routing

? Standalone Routing

Angular 15+ Standalone Components Routing

? Quick Overview

From Angular 15 onwards, you can build applications using standalone components and standalone routing, without creating any NgModule. Components declare their own dependencies via the imports array, and routing is configured using provideRouter() during bootstrap. This reduces boilerplate and makes apps easier to structure and scale.

? Key Concepts

  • Standalone Components — Components that set standalone: true and don’t belong to a module.
  • Local Imports — Each standalone component declares its dependencies in the imports array.
  • Route Configuration — Routes are defined in a simple Routes array (e.g., app.routes.ts).
  • provideRouter() — Registers routing at bootstrap time instead of via RouterModule.forRoot().
  • bootstrapApplication() — New bootstrap API that replaces platformBrowserDynamic().bootstrapModule() in standalone apps.

? Syntax & Structure

To enable standalone routing, you typically work with three main pieces:

  1. A standalone component (e.g., HomeComponent).
  2. A routes definition file (e.g., app.routes.ts).
  3. The bootstrap file (e.g., main.ts) that calls bootstrapApplication() with provideRouter(routes).

The following examples show how to create a standalone component and how to configure routing using the standalone APIs.

? Code Example 1 — Standalone Component

A simple HomeComponent that is fully standalone and ready for routing.

? View Code Example
// Standalone HomeComponent with router support
import { Component } from '@angular/core';
import { RouterModule } from '@angular/router';

@Component({
  selector: 'app-home',
  standalone: true,
  imports: [RouterModule],
  template: '<h2>Welcome to Home</h2>'
})
export class HomeComponent { }

? What This Component Does

  • standalone: true tells Angular this component is not part of any module.
  • imports: [RouterModule] lets the component use router directives (e.g., routerLink) directly if needed.
  • The template simply renders: <h2>Welcome to Home</h2>.
  • This component can be directly referenced in route definitions using component: HomeComponent.

?️ Code Example 2 — Standalone Routing Setup

Define your routes in app.routes.ts and then bootstrap the app with routing in main.ts.

? View Code Example
// app.routes.ts — define routes using standalone components
import { Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';

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

// main.ts — bootstrap app and provide router
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { AppComponent } from './app/app.component';
import { routes } from './app.routes';

bootstrapApplication(AppComponent, {
  providers: [provideRouter(routes)]
});

? Live Routing Flow (Conceptual)

  • When the URL is /, Angular matches the empty path ('') and displays HomeComponent.
  • When the URL is /about, Angular displays AboutComponent.
  • provideRouter(routes) wires the route configuration into the app at bootstrap time.
  • bootstrapApplication(AppComponent, ...) starts the Angular app using the new standalone bootstrap API.

? Tips & Best Practices

  • Use standalone components for small to medium apps to simplify module structure and reduce boilerplate.
  • Combine standalone components with lazy loading for better performance on large apps.
  • Take advantage of direct imports in components instead of maintaining large NgModule trees.
  • Keep route definitions in a dedicated file (like app.routes.ts) for better organization.
  • Use meaningful path names (e.g., 'about', 'contact') to keep URLs readable.

? Try It Yourself / Practice Tasks

  • Create a standalone ContactComponent with a simple contact form and set standalone: true.
  • Add a new route { path: 'contact', component: ContactComponent } to your routes array.
  • Update your main navigation (e.g., using routerLink) to navigate between /, /about, and /contact.
  • Experiment by removing NgModules and ensuring the app still works with only standalone components and routing.
  • Add lazy-loaded routes using loadComponent and observe how the bundle size changes.