Router Class
TheRouter 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 usingprovideRouter 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
NavigationStart- Navigation beginsRoutesRecognized- Routes matchedGuardsCheckStart- Guards execution beginsGuardsCheckEnd- Guards execution completesResolveStart- Resolvers execution beginsResolveEnd- Resolvers execution completesNavigationEnd- Navigation succeedsNavigationCancel- Navigation cancelledNavigationError- Navigation fails
routerState
url
navigated
false initially, true after first navigation.
config
currentNavigation
null when idle.
Example:
lastSuccessfulNavigation
null if no navigation has succeeded yet.
Methods
navigate()
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 toqueryParams- Query parameters for the URLfragment- URL fragment/hashqueryParamsHandling- How to handle query params ('merge','preserve', or'replace')skipLocationChange- Navigate without updating browser URLreplaceUrl- Replace current history entry instead of pushing new onestate- Developer-defined state to pass with navigation
true on success, false on failure.
Examples:
navigateByUrl()
string | UrlTree
required
Absolute URL path or UrlTree object.
NavigationBehaviorOptions
Options controlling navigation behavior:
skipLocationChange- Don’t update browser URLreplaceUrl- Replace history entrystate- State to pass with navigationonSameUrlNavigation- How to handle navigation to current URL
true on success, false on failure.
Examples:
createUrlTree()
any[]
required
Array of URL fragments.
Options for URL creation including
relativeTo, queryParams, fragment, etc.UrlTree object.
Examples:
serializeUrl()
parseUrl()
isActive()
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'
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()
initialNavigation()
setUpLocationChangeListener()
dispose()
Usage Examples
Basic Navigation
Navigation with Options
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
