Angular 15+ Standalone Components Routing
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.
standalone: true and don’t belong to a module.imports array.Routes array (e.g., app.routes.ts).RouterModule.forRoot().platformBrowserDynamic().bootstrapModule() in standalone apps.To enable standalone routing, you typically work with three main pieces:
HomeComponent).app.routes.ts).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.
A simple HomeComponent that is fully standalone and ready for routing.
// 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 { }
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.<h2>Welcome to Home</h2>.component: HomeComponent.Define your routes in app.routes.ts and then bootstrap the app with routing in main.ts.
// 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)]
});
/, Angular matches the empty path ('') and displays HomeComponent./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.imports in components instead of maintaining large NgModule trees.app.routes.ts) for better organization.'about', 'contact') to keep URLs readable.ContactComponent with a simple contact form and set standalone: true.{ path: 'contact', component: ContactComponent } to your routes array.routerLink) to navigate between /, /about, and /contact.NgModules and ensuring the app still works with only standalone components and routing.loadComponent and observe how the bundle size changes.