Skip to main content

Router Class

The Router service is the main entry point for Angular routing. It provides methods for navigation, URL manipulation, and access to routing state.

Import

Class Overview

Description

The Router service facilitates navigation among views and URL manipulation in Angular applications. It is provided in the root scope and configured using provideRouter or RouterModule.forRoot().
The Router service is automatically provided when you use provideRouter() in standalone apps or RouterModule.forRoot() in NgModule-based apps.

Properties

events

An observable stream of routing events. Subscribe to this to react to navigation lifecycle events. Example:
Event Types:
  • NavigationStart - Navigation begins
  • RoutesRecognized - Routes matched
  • GuardsCheckStart - Guards execution begins
  • GuardsCheckEnd - Guards execution completes
  • ResolveStart - Resolvers execution begins
  • ResolveEnd - Resolvers execution completes
  • NavigationEnd - Navigation succeeds
  • NavigationCancel - Navigation cancelled
  • NavigationError - Navigation fails

routerState

The current router state containing the activated route tree and routing information. Example:

url

The current URL as a string. Example:
Indicates whether at least one navigation event has occurred. false initially, true after first navigation.

config

The current route configuration array. Example:

currentNavigation

A signal that represents the current navigation object when navigating, null when idle. Example:

lastSuccessfulNavigation

A signal representing the most recent successful navigation, null if no navigation has succeeded yet.

Methods

Navigates based on the provided array of commands and options. Parameters:
any[]
required
Array of URL fragments to construct the target URL. Can include path segments, parameters, and outlets.
NavigationExtras
Options that control navigation behavior:
  • relativeTo - Route to navigate relative to
  • queryParams - Query parameters for the URL
  • fragment - URL fragment/hash
  • queryParamsHandling - How to handle query params ('merge', 'preserve', or 'replace')
  • skipLocationChange - Navigate without updating browser URL
  • replaceUrl - Replace current history entry instead of pushing new one
  • state - Developer-defined state to pass with navigation
Returns: Promise that resolves to true on success, false on failure. Examples:
Navigates to a view using an absolute URL path or UrlTree. Parameters:
string | UrlTree
required
Absolute URL path or UrlTree object.
NavigationBehaviorOptions
Options controlling navigation behavior:
  • skipLocationChange - Don’t update browser URL
  • replaceUrl - Replace history entry
  • state - State to pass with navigation
  • onSameUrlNavigation - How to handle navigation to current URL
Returns: Promise that resolves to true on success, false on failure. Examples:

createUrlTree()

Creates a UrlTree object from an array of commands without navigating. Parameters:
any[]
required
Array of URL fragments.
UrlCreationOptions
Options for URL creation including relativeTo, queryParams, fragment, etc.
Returns: A UrlTree object. Examples:

serializeUrl()

Converts a UrlTree to a string URL. Example:

parseUrl()

Parses a string URL into a UrlTree object. Example:

isActive()

Checks if a given URL is currently active. Parameters:
string | UrlTree
required
URL to check.
IsActiveMatchOptions
required
Matching options:
  • paths - 'exact' or 'subset'
  • queryParams - 'exact', 'subset', or 'ignored'
  • fragment - 'exact' or 'ignored'
  • matrixParams - 'exact', 'subset', or 'ignored'
Returns: true if URL is active, false otherwise. Example:
The isActive method is deprecated in favor of using the standalone isActive function from @angular/router.

resetConfig()

Resets the route configuration. Useful for dynamic route updates. Example:

initialNavigation()

Performs the initial navigation. Called automatically by the Router during application bootstrap.

setUpLocationChangeListener()

Sets up the listener for browser location changes (back/forward buttons).

dispose()

Disposes of the router, cleaning up subscriptions and event listeners.

Usage Examples

Basic Navigation

Listening to Navigation Events

Using UrlTree for Redirects

See Also

Route Configuration

Learn about configuring routes

Route Guards

Protect routes with guards

Routing Guide

Complete routing guide

Navigation Events

Router event types and usage