@title
Routing & Navigation
@intro
Discover the basics of screen navigation with the Angular Router.
@description
The Angular **`Router`** enables navigation from one [view](guide/glossary#view) to the next
as users perform application tasks.
This guide covers the router's primary features, illustrating them through the evolution
of a small application that you can run live in the browser.
## Overview
The browser is a familiar model of application navigation:
* Enter a URL in the address bar and the browser navigates to a corresponding page.
* Click links on the page and the browser navigates to a new page.
* Click the browser's back and forward buttons and the browser navigates
backward and forward through the history of pages you've seen.
The Angular `Router` ("the router") borrows from this model.
It can interpret a browser URL as an instruction to navigate to a client-generated view.
It can pass optional parameters along to the supporting view component that help it decide what specific content to present.
You can bind the router to links on a page and it will navigate to
the appropriate application view when the user clicks a link.
You can navigate imperatively when the user clicks a button, selects from a drop box,
or in response to some other stimulus from any source. And the router logs activity
in the browser's history journal so the back and forward buttons work as well.
# Contents
* [The Basics](guide/router#basics)
* [``](guide/router#basics-base-href)
* [Router imports](guide/router#basics-router-imports)
* [Configuration](guide/router#basics-config)
* [Router outlet](guide/router#basics-router-outlet)
* [Router links](guide/router#basics-router-links)
* [Router state](guide/router#basics-router-state)
* [Summary](guide/router#basics-summary)
* [The sample application](guide/router#sample-app-intro)
* [Milestone 1: Getting started with the router](guide/router#getting-started)
* [Setting the base href](guide/router#base-href)
* [Importing from the router library](guide/router#import)
* [Define routes](guide/router#route-config)
* [The `AppComponent` shell](guide/router#shell)
* [RouterOutlet](guide/router#router-outlet)
* [`RouterLink binding`](guide/router#router-link)
* [`RouterLinkActive` binding](guide/router#router-link-active)
* [Wildcard route](guide/router#wildcard)
* [The default route to heroes](guide/router#default-route)
* [Milestone 2: Routing module](guide/router#routing-module)
* [Refactor the routing configuration into a routing module](guide/router#routing-refactor)
* [Do you need a Routing Module?](guide/router#why-routing-module)
* [Milestone 3: Heroes feature](guide/router#heroes-feature)
* [Add heroes functionality](guide/router#heroes-functionality)
* [Hero feature routing requirements](guide/router#hero-routing-requirements)
* [Hero feature route configuration](guide/router#hero-routing-module)
* [Add the routing module to the _HeroesModule_](guide/router#adding-routing-module)
* [Remove duplicate hero routes](guide/router#remove-duplicate-hero-routes)
* [Import hero module into AppModule](guide/router#merge-hero-routes)
* [Module import order matters](guide/router#routing-module-order)
* [Route Definition with a parameter](guide/router#route-def-with-parameter)
* [Navigate to hero detail imperatively](guide/router#navigate)
* [Setting the route parameters in the list view](guide/router#route-parameters)
* [ActivatedRoute: the one-stop-shop for route information](guide/router#activated-route)
* [Observable params and component reuse](guide/router#reuse)
* [Snapshot: the _no-observable_ alternative](guide/router#snapshot)
* [Navigating back to the list component](guide/router#nav-to-list)
* [Route Parameters: Required or optional?](guide/router#optional-route-parameters)
* [Heroes list: optionally selecting a hero](guide/router#optionally-selecting)
* [Route parameters in the *ActivatedRoute* service](guide/router#route-parameters-activated-route)
* [Adding animations to the routed component](guide/router#route-animation)
* [Milestone 3 wrap up](guide/router#milestone-3-wrap-up)
* [Milestone 4: Crisis center feature](guide/router#milestone-4)
* [A crisis center with child routes](guide/router#crisis-child-routes)
* [Child routing component](guide/router#child-routing-component)
* [Child route configuration](guide/router#child-route-config)
* [Import crisis center module into the _AppModule_ routes](guide/router#import-crisis-module)
* [Relative navigation](guide/router#relative-navigation)
* [Navigate to crisis detail with a relative URL](guide/router#nav-to-crisis)
* [Displaying multiple routes in named outlets](guide/router#named-outlets)
* [Secondary routes](guide/router#secondary-routes)
* [Add a secondary route](guide/router#add-secondary-route)
* [Secondary route navigation: merging routes during navigation](guide/router#secondary-route-navigation)
* [Clearing secondary routes](guide/router#clear-secondary-routes)
* [Milestone 5: Route guards](guide/router#guards)
* [`CanActivate`: requiring authentication](guide/router#can-activate-guard)
* [Component-less route: grouping routes without a component](guide/router#component-less-route)
* [Guard the admin feature](guide/router#guard-admin-feature)
* [Teach *AuthGuard* to authenticate](guide/router#teach-auth)
* [Add the login component](guide/router#add-login-component)
* [`CanActivateChild`: guarding child routes](guide/router#can-activate-child-guard)
* [`CanDeactivate`: handling unsaved changes](guide/router#can-deactivate-guard)
* [Cancel and save](guide/router#cancel-save)
* [`Resolve`: pre-fetching component data](guide/router#resolve-guard)
* [Fetch data before navigating](guide/router#fetch-before-navigating)
* [Query parameters and fragments](guide/router#query-parameters)
* [Milestone 6: Asynchronous routing](guide/router#asynchronous-routing)
* [Lazy loading route configuration](guide/router#lazy-loading-route-config)
* [CanLoad Guard: guarding unauthorized loading of feature modules](guide/router#can-load-guard)
* [Preloading: background loading of feature areas](guide/router#preloading)
* [How preloading works](guide/router#how-preloading)
* [Lazy load the crisis center](guide/router#lazy-load-crisis-center)
* [_CanLoad_ blocks preload](guide/router#preload-canload)
* [Custom Preloading Strategy](guide/router#custom-preloading)
* [Inspect the router's configuration](guide/router#inspect-config)
* [Wrap up and final app](guide/router#final-app)
* [Appendices](guide/router#appendices)
* [Appendix: link parameters array](guide/router#link-parameters-array)
* [Appendix: *LocationStrategy* and browser URL styles](guide/router#location-strategy)
{@a basics}
## The Basics
This guide proceeds in phases, marked by milestones, starting from a simple two-pager
and building toward a modular, multi-view design with child routes.
An introduction to a few core router concepts will help orient you to the details that follow.
{@a basics-base-href}
### *<base href>*
Most routing applications should add a `` element to the `index.html` as the first child in the `
` tag
to tell the router how to compose navigation URLs.
If the `app` folder is the application root, as it is for the sample application,
set the `href` value *exactly* as shown here.
Router Part
|
Meaning
|
Router
|
Displays the application component for the active URL.
Manages navigation from one component to the next.
|
RouterModule
|
A separate Angular module that provides the necessary service providers
and directives for navigating through application views.
|
Routes
|
Defines an array of Routes, each mapping a URL path to a component.
|
Route
|
Defines how the router should navigate to a component based on a URL pattern.
Most routes consist of a path and a component type.
|
RouterOutlet
|
The directive (<router-outlet> ) that marks where the router displays a view.
|
RouterLink
|
The directive for binding a clickable HTML element to
a route. Clicking an element with a routerLink directive
that is bound to a string or a link parameters array triggers a navigation.
|
RouterLinkActive
|
The directive for adding/removing classes from an HTML element when an associated
routerLink contained on or inside the element becomes active/inactive.
|
ActivatedRoute
|
A service that is provided to each route component that contains route specific
information such as route parameters, static data, resolve data, global query params, and the global fragment.
|
RouterState
|
The current state of the router including a tree of the currently activated
routes together with convenience methods for traversing the route tree.
|
Link parameters array
|
An array that the router interprets as a routing instruction.
You can bind that array to a RouterLink or pass the array as an argument to
the Router.navigate method.
|
Routing component
|
An Angular component with a RouterOutlet that displays views based on router navigations.
|
{@a sample-app-intro}
## The sample application
This guide describes development of a multi-page routed sample application.
Along the way, it highlights design decisions and describes key features of the router such as:
* Organizing the application features into modules.
* Navigating to a component (*Heroes* link to "Heroes List").
* Including a route parameter (passing the Hero `id` while routing to the "Hero Detail").
* Child routes (the *Crisis Center* has its own routes).
* The `CanActivate` guard (checking route access).
* The `CanActivateChild` guard (checking child route access).
* The `CanDeactivate` guard (ask permission to discard unsaved changes).
* The `Resolve` guard (pre-fetching route data).
* Lazy loading feature modules.
* The `CanLoad` guard (check before loading feature module assets).
The guide proceeds as a sequence of milestones as if you were building the app step-by-step.
But, it is not a tutorial and it glosses over details of Angular application construction
that are more thoroughly covered elsewhere in the documentation.
The full source for the final version of the app can be seen and downloaded from the