@title
Routing & Navigation
@intro
Discover the basics of screen navigation with the Angular Router.
@description
The Angular **`Router`** enables navigation from one [view](./glossary.html#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.
You'll learn many router details in this guide which covers
* Setting the [base href](#base-href)
* Importing from the [router library](#import)
* [Configuring the router](#route-config)
* Handling unmatched URLs with a [wildcard route](#wildcard-route)
* The [link parameters array](#link-parameters-array) that propels router navigation
* Setting the [default route](#default-route) where the application navigates at launch
* [Redirecting](#redirect) from one route to another
* Navigating when the user clicks a data-bound [RouterLink](#router-link)
* Navigating under [program control](#navigate)
* Retrieving information from the [route](#activated-route)
* [Animating](#route-animation) transitions for route components
* Navigating [relative](#relative-navigation) to the current URL
* Toggling css classes for the [active router link](#router-link-active)
* Embedding critical information in the URL with [route parameters](#route-parameters)
* Providing non-critical information in [optional route parameters](#optional-route-parameters)
* Refactoring routing into a [routing module](#routing-module)
* [Importing routing modules in the proper order](#routing-module-order)
* Add [child routes](#child-routing-component) under a feature section
* [Grouping child routes](#component-less-route) without a component
* Displaying [multiple routes](#named-outlets) in separate outlets
* Confirming or canceling navigation with [guards](#guards)
* [CanActivate](#can-activate-guard) to prevent navigation to a route
* [CanActivateChild](#can-activate-child-guard) to prevent navigation to a child route
* [CanDeactivate](#can-deactivate-guard) to prevent navigation away from the current route
* [Resolve](#resolve-guard) to pre-fetch data before activating a route
* [CanLoad](#can-load-guard) to prevent asynchronous routing
* Providing optional information across routes with [query parameters](#query-parameters)
* Jumping to anchor elements using a [fragment](#fragment)
* Loading feature areas [asynchronously](#asynchronous-routing)
* Preloading feature areas [during navigation](#preloading)
* Using a [custom strategy](#custom-preloading) to only preload certain features
* [Inspect the router's configuration](#inspect-config).
* Choosing the "HTML5" or "hash" [URL style](#browser-url-styles)
## 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.
### *<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 imports
The Angular Router is an optional service that presents a particular component view for a given URL.
It is not part of the Angular core. It is in its own library package, `@angular/router`.
Import what you need from it as you would from any other Angular package.
You'll learn about more options in the [details below](#browser-url-styles).### Configuration
A routed Angular application has one, singleton instance of the *`Router`* service.
When the browser's URL changes, that router looks for a corresponding `Route`
from which it can determine the component to display.
A router has no routes until you configure it.
The following example creates four route definitions, configures the router via the `RouterModule.forRoot` method,
and adds the result to the `AppModule`'s `imports` array.
{@a example-config}
The `appRoutes` array of *routes* describes how to navigate.
Pass it to the `RouterModule.forRoot` method in the module `imports` to configure the router.
Each `Route` maps a URL `path` to a component.
There are _no leading slashes_ in the _path_.
The router parses and builds the final URL for you,
allowing you to use both relative and "absolute" paths when navigating between application views.
The `:id` in the first route is a token for a route parameter. In a URL such as `/hero/42`, "42"
is the value of the `id` parameter. The corresponding `HeroDetailComponent`
will use that value to find and present the hero whose `id` is 42.
You'll learn more about route parameters later in this guide.
The `data` property in the third route is a place to store arbitrary data associated with
this specific route. The data property is accessible within each activated route. Use it to store
items such as page titles, breadcrumb text, and other read-only, _static_ data.
You'll use the [resolve guard](#resolve-guard) to retrieve _dynamic_ data later in the guide.
The `empty path` in the fourth route represents the default path for the application,
the place to go when the path in the URL is empty, as it typically is at the start.
This default route redirects to the route for the `/heroes` URL and, therefore, will display the `HeroesListComponent`.
The `**` path in the last route is a **wildcard**. The router will select this route
if the requested URL doesn't match any paths for routes defined earlier in the configuration.
This is useful for displaying a "404 - Not Found" page or redirecting to another route.
**The order of the routes in the configuration matters** and this is by design. The router uses a **first-match wins**
strategy when matching routes, so more specific routes should be placed above less specific routes.
In the configuration above, routes with a static path are listed first, followed by an empty path route,
that matches the default route.
The wildcard route comes last because it matches _every URL_ and should be selected _only_ if no other routes are matched first.
### Router outlet
Given this configuration, when the browser URL for this application becomes `/heroes`,
the router matches that URL to the route path `/heroes` and displays the `HeroListComponent`
_after_ a `RouterOutlet` that you've placed in the host view's HTML.
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 should display 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.
|
## 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