angular/angular
@angular/router
Client-side routing: URL ↔ component-tree mapping, navigation lifecycle events, route guards, route data, and lazy/standalone route configuration.
Purpose
The router is responsible for:
- Matching the current URL against a
Routesarray and building the resultingActivatedRouteSnapshottree. - Performing navigation: running guards, resolvers, lazy-loading any matched routes, then activating the new component tree in the matching
RouterOutlets. - Emitting an
Observable<Event>onRouter.eventsso apps can observe and react to the navigation lifecycle (NavigationStart,RoutesRecognized,GuardsCheckStart,NavigationEnd, etc.). - Parsing and serializing URLs through
UrlTree/UrlSerializer. - Generating links via the
routerLinkdirective and theRouter.createUrlTreeAPI.
Directory layout
packages/router/
├── src/
│ ├── router.ts # The Router service
│ ├── router_module.ts # RouterModule (legacy NgModule API)
│ ├── provide_router.ts # provideRouter standalone API
│ ├── router_outlet.ts # <router-outlet>
│ ├── directives/ # routerLink, routerLinkActive
│ ├── url_tree.ts # UrlTree, UrlSegment
│ ├── shared.ts # PRIMARY_OUTLET, etc.
│ ├── route_reuse_strategy.ts # RouteReuseStrategy
│ ├── recognize.ts # URL → ActivatedRouteSnapshot tree
│ ├── apply_redirects.ts # Redirects evaluation
│ ├── operators/ # RxJS operators used by navigation
│ ├── events.ts # Navigation event types
│ ├── router_state.ts # ActivatedRoute / Snapshot
│ ├── router_preloader.ts # PreloadAllModules + custom strategies
│ ├── transfer_state.ts # SSR-side state transfer hooks
│ └── ...
├── upgrade/ # Hybrid Angular.js routing helpers
├── testing/ # RouterTestingHarness
└── schematics/migrations/ # provideRouter, withRouterConfig migrationsKey abstractions
| Type / function | File | What it is |
|---|---|---|
Router |
packages/router/src/router.ts |
The service that drives navigation. |
provideRouter |
packages/router/src/provide_router.ts |
The standalone-bootstrap registration entry. Replaces RouterModule.forRoot. |
RouterOutlet |
packages/router/src/router_outlet.ts |
The placeholder where matched components render. |
Routes |
packages/router/src/models.ts |
Array type describing a route configuration. |
ActivatedRoute, ActivatedRouteSnapshot |
packages/router/src/router_state.ts |
Per-route info available to components and guards. |
routerLink |
packages/router/src/directives/router_link.ts |
The link directive. |
RouterStateSnapshot |
router_state.ts |
The whole router-state tree at a moment in time. |
UrlTree / UrlSegment / UrlSerializer |
packages/router/src/url_tree.ts, url_handling_strategy.ts |
URL parsing and serialization. |
PreloadingStrategy, PreloadAllModules |
packages/router/src/router_preloader.ts |
Lazy-route preloaders. |
Navigation pipeline
graph TD Trigger["Navigation trigger<br/>(routerLink, navigate(), popstate)"] --> Recognize["recognize()<br/>URL → snapshot tree"] Recognize --> Redirects["applyRedirects()"] Redirects --> Lazy["Load lazy chunks<br/>(loadChildren, loadComponent)"] Lazy --> Guards["CanActivate / CanLoad / CanMatch"] Guards --> Resolvers["Resolve data"] Resolvers --> Activate["Activate components<br/>(swap in RouterOutlets)"] Activate --> Events["Emit NavigationEnd"]
Each stage emits matching Events on Router.events. The pipeline is implemented as a sequence of RxJS operators in packages/router/src/operators/, composed in Router.scheduleNavigation.
Standalone vs NgModule
The standalone API (provideRouter) is the recommended entry point:
bootstrapApplication(AppRoot, {
providers: [
provideRouter(routes, withComponentInputBinding(), withInMemoryScrolling()),
],
});provideRouter accepts feature functions (withDebugTracing, withRouterConfig, withInMemoryScrolling, withHashLocation, withPreloading, etc.) defined in provide_router.ts.
RouterModule.forRoot(routes, options) is still supported for legacy code; an ng update-driven migration converts most call sites automatically.
Integration points
@angular/common— depends onLocationandLocationStrategyfor URL manipulation.@angular/platform-browser— registers the browser-specificLocationStrategy.@angular/platform-server— feeds the initial URL into the router during SSR viaINITIAL_CONFIG.- Component input binding —
withComponentInputBinding()exposes route params/data as@Input()properties on routed components, removing most directActivatedRouteinjection. - Component-level signal queries —
withComponentInputBinding()is signal-aware; signal-based inputs receive route values automatically.
Migrations
packages/router/schematics/ contains migrations to:
- Convert
RouterModule.forRoot(...)toprovideRouter(...). - Replace router-related
importpaths after refactors. - Adopt new feature functions like
withRouterConfig.
Entry points for modification
- A new feature function for
provideRouter: define awithFoo()function inprovide_router.ts, backed by anEnvironmentProvidersarray. The naming and shape are enforced by review. - A new navigation event: extend
Eventinevents.ts, emit it from the matching pipeline operator, and document it in the JSDoc. - A new directive (similar to
routerLink): add it underdirectives/and export frompublic_api.ts.
For testing, prefer RouterTestingHarness from packages/router/testing over manual RouterTestingModule setup. The harness handles the zoneless waiting pattern correctly.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.