The Angular Router enables navigation between different views or components in a single-page application. It interprets browser URLs and maps them to the corresponding component views. The router supports deep linking, lazy loading, guards, resolvers, and nested routes.
Configure the router at the application root level using RouterModule.forRoot in the root module (or provideRouter for standalone apps):
import { RouterModule, Routes } from '@angular/router';
const routes: Routes = [
{ path: '', component: HomeComponent },
{ path: 'about', component: AboutComponent },
{ path: 'products', component: ProductListComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
RouterModule.forChild(routes) in feature modules to register child routes without re-creating the router service.Each route is an object with several properties:
const routes: Routes = [
{ path: '', component: HomeComponent, pathMatch: 'full' },
{ path: 'products', component: ProductListComponent },
{ path: 'products/:id', component: ProductDetailComponent },
{ path: 'category/:categoryName', component: CategoryComponent },
{ path: '**', component: NotFoundComponent } // Wildcard — must be last
];
Redirect from one path to another:
{ path: 'old-products', redirectTo: '/products', pathMatch: 'full' }
Catches all unmatched routes — typically used for a 404 page. Must be defined last in the routes array.
Pass dynamic values via the URL path:
// Route config
{ path: 'users/:userId', component: UserDetailComponent }
// Component
import { ActivatedRoute } from '@angular/router';
ngOnInit() {
this.route.params.subscribe(params => {
this.userId = params['userId'];
this.loadUser();
});
}
Access query parameters (?page=1&sort=name) and fragments (#section):
// Reading
this.route.queryParams.subscribe(params => {
this.page = params['page'];
});
this.route.fragment.subscribe(f => this.section = f);
// Navigation
this.router.navigate(['/products'], {
queryParams: { page: 2, sort: 'price' },
fragment: 'reviews'
});
Create hierarchies of routes with child components:
const routes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
children: [
{ path: '', redirectTo: 'overview', pathMatch: 'full' },
{ path: 'overview', component: OverviewComponent },
{ path: 'analytics', component: AnalyticsComponent },
{ path: 'settings', component: SettingsComponent }
]
}
];
The parent component must include a <router-outlet> to render child routes.
Navigate using declarative links in templates:
<nav>
<a routerLink="/dashboard" routerLinkActive="active">Dashboard</a>
<a routerLink="/products" routerLinkActive="active"
[routerLinkActiveOptions]="{ exact: false }">Products</a>
<a [routerLink]="['/users', user.id]">User Profile</a>
</nav>
routerLinkActive adds a CSS class (e.g., active) when the current URL matches the link. Use exact: true for exact path matching.
Navigate imperatively in your component class:
constructor(private router: Router) { }
goToProduct(id: number) {
this.router.navigate(['/products', id]);
}
goToDashboard() {
this.router.navigateByUrl('/dashboard');
}
Protect routes with guards. Common guard types: CanActivate, CanActivateChild, CanDeactivate, Resolve, and CanLoad.
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
constructor(private auth: AuthService, private router: Router) { }
canActivate(): boolean {
if (this.auth.isLoggedIn()) return true;
this.router.navigate(['/login']);
return false;
}
}
// Route config
{ path: 'admin', component: AdminComponent, canActivate: [AuthGuard] }
Load feature modules on demand to reduce initial bundle size:
{
path: 'admin',
loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule)
}
Angular splits the admin module into a separate chunk, loaded only when the user navigates to /admin.
This chapter covered the Angular Router: route configuration, parameterized routes, query parameters and fragments, nested routes, routerLink and routerLinkActive, programmatic navigation, route guards (CanActivate), and lazy loading with loadChildren. With these tools, you can build complex navigation flows for SPAs.
?category=electronics&sort=price).