2017-11-06 13:02:18 -05:00
# Routing
2017-03-31 19:57:13 -04:00
2017-03-27 11:08:53 -04:00
There are new requirements for the Tour of Heroes app:
2017-02-22 13:09:39 -05:00
* Add a *Dashboard* view.
2017-03-27 11:08:53 -04:00
* Add the ability to navigate between the *Heroes* and *Dashboard* views.
* When users click a hero name in either view, navigate to a detail view of the selected hero.
* When users click a *deep link* in an email, open the detail view for a particular hero.
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
When you’ re done, users will be able to navigate the app like this:
2017-02-22 13:09:39 -05:00
2017-05-09 18:53:32 -04:00
< figure >
2017-04-11 16:44:52 -04:00
2017-11-06 13:02:18 -05:00
< img src = 'generated/images/guide/toh/nav-diagram.png' alt = "View navigations" >
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
< / figure >
2017-03-27 11:08:53 -04:00
2018-02-22 11:08:07 -05:00
## Add the `AppRoutingModule`
2017-03-31 19:57:13 -04:00
2018-12-14 03:36:26 -05:00
In Angular, the best practice is to load and configure the router in a separate, top-level module
2017-11-06 13:02:18 -05:00
that is dedicated to routing and imported by the root `AppModule` .
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
By convention, the module class name is `AppRoutingModule` and it belongs in the `app-routing.module.ts` in the `src/app` folder.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
Use the CLI to generate it.
2017-03-31 19:57:13 -04:00
< code-example language = "sh" class = "code-shell" >
2017-11-06 13:02:18 -05:00
ng generate module app-routing --flat --module=app
2017-03-31 19:57:13 -04:00
< / code-example >
2018-07-19 18:00:08 -04:00
< div class = "alert is-helpful" >
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
`--flat` puts the file in `src/app` instead of its own folder.< br >
`--module=app` tells the CLI to register it in the `imports` array of the `AppModule` .
2017-04-10 11:51:13 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
The generated file looks like this:
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.0.ts" header = "src/app/app-routing.module.ts (generated)" >
2017-03-27 11:08:53 -04:00
< / code-example >
2019-01-31 14:25:30 -05:00
Replace it with the following:
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.1.ts" header = "src/app/app-routing.module.ts (updated)" >
2017-03-27 11:08:53 -04:00
< / code-example >
2019-01-31 14:25:30 -05:00
First, `AppRoutingModule` imports `RouterModule` and `Routes` so the app can have routing functionality. The next import, `HeroesComponent` , will give the Router somewhere to go once you configure the routes.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
Notice that the `CommonModule` references and `declarations` array are unecessary, so are no
longer part of `AppRoutingModule` . The following sections explain the rest of the `AppRoutingModule` in more detail.
2017-02-22 13:09:39 -05:00
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
### Routes
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
The next part of the file is where you configure your routes.
*Routes* tell the Router which view to display when a user clicks a link or
pastes a URL into the browser address bar.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
Since `AppRoutingModule` already imports `HeroesComponent` , you can use it in the `routes` array:
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.ts" header = "src/app/app-routing.module.ts"
2017-11-06 13:02:18 -05:00
region="heroes-route">
< / code-example >
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
A typical Angular `Route` has two properties:
* `path` : a string that matches the URL in the browser address bar.
* `component` : the component that the router should create when navigating to this route.
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
This tells the router to match that URL to `path: 'heroes'`
and display the `HeroesComponent` when the URL is something like `localhost:4200/heroes` .
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
### `RouterModule.forRoot()`
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
The `@NgModule` metadata initializes the router and starts it listening for browser location changes.
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
The following line adds the `RouterModule` to the `AppRoutingModule` `imports` array and
configures it with the `routes` in one step by calling
`RouterModule.forRoot()` :
< code-example path = "toh-pt5/src/app/app-routing.module.ts" header = "src/app/app-routing.module.ts" region = "ngmodule-imports" >
2017-11-06 13:02:18 -05:00
< / code-example >
2017-02-22 13:09:39 -05:00
2018-07-19 18:00:08 -04:00
< div class = "alert is-helpful" >
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
The method is called `forRoot()` because you configure the router at the application's root level.
2019-01-31 14:25:30 -05:00
The `forRoot()` method supplies the service providers and directives needed for routing,
2017-11-06 13:02:18 -05:00
and performs the initial navigation based on the current browser URL.
2017-03-31 07:23:16 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-31 07:23:16 -04:00
2019-01-31 14:25:30 -05:00
Next, `AppRoutingModule` exports `RouterModule` so it will be available throughout the app.
2017-03-31 19:57:13 -04:00
2019-04-17 12:46:23 -04:00
Open the `AppComponent` template and replace the `<app-heroes>` element with a `<router-outlet>` element.
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.ts" header = "src/app/app-routing.module.ts (exports array)" region = "export-routermodule" >
2017-03-31 07:23:16 -04:00
< / code-example >
2019-01-31 14:25:30 -05:00
## Add `RouterOutlet`
Open the `AppComponent` template and replace the `<app-heroes>` element with a `<router-outlet>` element.
< code-example path = "toh-pt5/src/app/app.component.html" region = "outlet" header = "src/app/app.component.html (router-outlet)" >
< / code-example >
The `AppComponent` template no longer needs `<app-heroes>` because the app will only display the `HeroesComponent` when the user navigates to it.
2017-03-31 07:23:16 -04:00
2017-11-06 13:02:18 -05:00
The `<router-outlet>` tells the router where to display routed views.
2017-03-31 07:23:16 -04:00
2018-07-19 18:00:08 -04:00
< div class = "alert is-helpful" >
2017-03-31 07:23:16 -04:00
2017-11-06 13:02:18 -05:00
The `RouterOutlet` is one of the router directives that became available to the `AppComponent`
because `AppModule` imports `AppRoutingModule` which exported `RouterModule` .
2017-03-27 11:08:53 -04:00
2017-04-10 11:51:13 -04:00
< / div >
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
#### Try it
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
You should still be running with this CLI command.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
< code-example language = "sh" class = "code-shell" >
ng serve
2017-03-27 11:08:53 -04:00
< / code-example >
2017-11-06 13:02:18 -05:00
The browser should refresh and display the app title but not the list of heroes.
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
Look at the browser's address bar.
2017-11-06 13:02:18 -05:00
The URL ends in `/` .
The route path to `HeroesComponent` is `/heroes` .
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Append `/heroes` to the URL in the browser address bar.
You should see the familiar heroes master/detail view.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
{@a routerlink}
2017-03-31 19:57:13 -04:00
2018-02-22 11:08:07 -05:00
## Add a navigation link (`routerLink`)
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
Ideally, users should be able to click a link to navigate rather
than pasting a route URL into the address bar.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
Add a `<nav>` element and, within that, an anchor element that, when clicked,
2017-11-06 13:02:18 -05:00
triggers navigation to the `HeroesComponent` .
The revised `AppComponent` template looks like this:
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app.component.html" region = "heroes" header = "src/app/app.component.html (heroes RouterLink)" >
2017-11-06 13:02:18 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
A [`routerLink` attribute ](#routerlink ) is set to `"/heroes"` ,
the string that the router matches to the route to `HeroesComponent` .
2019-01-31 14:25:30 -05:00
The `routerLink` is the selector for the [`RouterLink` directive ](/api/router/RouterLink )
2017-11-06 13:02:18 -05:00
that turns user clicks into router navigations.
It's another of the public directives in the `RouterModule` .
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
The browser refreshes and displays the app title and heroes link,
2017-11-06 13:02:18 -05:00
but not the heroes list.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
Click the link.
2017-11-06 13:02:18 -05:00
The address bar updates to `/heroes` and the list of heroes appears.
2017-03-27 11:08:53 -04:00
2018-07-19 18:00:08 -04:00
< div class = "alert is-helpful" >
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
Make this and future navigation links look better by adding private CSS styles to `app.component.css`
as listed in the [final code review ](#appcomponent ) below.
2017-02-22 13:09:39 -05:00
2017-04-10 11:51:13 -04:00
< / div >
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
## Add a dashboard view
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Routing makes more sense when there are multiple views.
2019-01-31 14:25:30 -05:00
So far there's only the heroes view.
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
Add a `DashboardComponent` using the CLI:
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
< code-example language = "sh" class = "code-shell" >
ng generate component dashboard
2017-03-27 11:08:53 -04:00
< / code-example >
2017-11-06 13:02:18 -05:00
The CLI generates the files for the `DashboardComponent` and declares it in `AppModule` .
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
Replace the default file content in these three files as follows:
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
< code-tabs >
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/dashboard/dashboard.component.html" path="toh-pt5/src/app/dashboard/dashboard.component.1.html">
2017-11-06 13:02:18 -05:00
< / code-pane >
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/dashboard/dashboard.component.ts" path="toh-pt5/src/app/dashboard/dashboard.component.ts">
2017-11-06 13:02:18 -05:00
< / code-pane >
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/dashboard/dashboard.component.css" path="toh-pt5/src/app/dashboard/dashboard.component.css">
2017-11-06 13:02:18 -05:00
< / code-pane >
< / code-tabs >
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
The _template_ presents a grid of hero name links.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
* The `*ngFor` repeater creates as many links as are in the component's `heroes` array.
* The links are styled as colored blocks by the `dashboard.component.css` .
* The links don't go anywhere yet but [they will shortly ](#hero-details ).
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
The _class_ is similar to the `HeroesComponent` class.
* It defines a `heroes` array property.
* The constructor expects Angular to inject the `HeroService` into a private `heroService` property.
2019-01-31 14:25:30 -05:00
* The `ngOnInit()` lifecycle hook calls `getHeroes()` .
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
This `getHeroes()` returns the sliced list of heroes at positions 1 and 5, returning only four of the Top Heroes (2nd, 3rd, 4th, and 5th).
2017-03-30 15:04:18 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/dashboard/dashboard.component.ts" header = "src/app/dashboard/dashboard.component.ts" region = "getHeroes" >
2017-03-31 07:23:16 -04:00
< / code-example >
2017-11-06 13:02:18 -05:00
### Add the dashboard route
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
To navigate to the dashboard, the router needs an appropriate route.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Import the `DashboardComponent` in the `AppRoutingModule` .
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.ts" region = "import-dashboard" header = "src/app/app-routing.module.ts (import DashboardComponent)" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-11-06 13:02:18 -05:00
Add a route to the `AppRoutingModule.routes` array that matches a path to the `DashboardComponent` .
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.ts" header = "src/app/app-routing.module.ts" region = "dashboard-route" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-11-06 13:02:18 -05:00
### Add a default route
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
When the app starts, the browser's address bar points to the web site's root.
2017-11-06 13:02:18 -05:00
That doesn't match any existing route so the router doesn't navigate anywhere.
The space below the `<router-outlet>` is blank.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
To make the app navigate to the dashboard automatically, add the following
route to the `AppRoutingModule.Routes` array.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.ts" header = "src/app/app-routing.module.ts" region = "redirect-route" >
2017-11-06 13:02:18 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
This route redirects a URL that fully matches the empty path to the route whose path is `'/dashboard'` .
2017-03-31 19:57:13 -04:00
2017-11-12 13:17:13 -05:00
After the browser refreshes, the router loads the `DashboardComponent`
2017-11-06 13:02:18 -05:00
and the browser address bar shows the `/dashboard` URL.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
### Add dashboard link to the shell
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
The user should be able to navigate back and forth between the
`DashboardComponent` and the `HeroesComponent` by clicking links in the
navigation area near the top of the page.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
Add a dashboard navigation link to the `AppComponent` shell template, just above the *Heroes* link.
2017-03-27 11:08:53 -04:00
2018-10-11 07:29:59 -04:00
< code-example path = "toh-pt5/src/app/app.component.html" header = "src/app/app.component.html" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
After the browser refreshes you can navigate freely between the two views by clicking the links.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
{@a hero-details}
## Navigating to hero details
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
The `HeroDetailsComponent` displays details of a selected hero.
At the moment the `HeroDetailsComponent` is only visible at the bottom of the `HeroesComponent`
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
The user should be able to get to these details in three ways.
2017-11-27 16:38:30 -05:00
2017-11-06 13:02:18 -05:00
1. By clicking a hero in the dashboard.
1. By clicking a hero in the heroes list.
1. By pasting a "deep link" URL into the browser address bar that identifies the hero to display.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
In this section, you'll enable navigation to the `HeroDetailsComponent`
and liberate it from the `HeroesComponent` .
2017-03-31 19:57:13 -04:00
2018-02-22 11:08:07 -05:00
### Delete _hero details_ from `HeroesComponent`
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
When the user clicks a hero item in the `HeroesComponent` ,
the app should navigate to the `HeroDetailComponent` ,
replacing the heroes list view with the hero detail view.
The heroes list view should no longer show hero details as it does now.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
Open the `HeroesComponent` template (`heroes/heroes.component.html`) and
delete the `<app-hero-detail>` element from the bottom.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
Clicking a hero item now does nothing.
2017-11-06 13:02:18 -05:00
You'll [fix that shortly ](#heroes-component-links ) after you enable routing to the `HeroDetailComponent` .
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
### Add a _hero detail_ route
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
A URL like `~/detail/11` would be a good URL for navigating to the *Hero Detail* view of the hero whose `id` is `11` .
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
Open `AppRoutingModule` and import `HeroDetailComponent` .
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.ts" region = "import-herodetail" header = "src/app/app-routing.module.ts (import HeroDetailComponent)" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
Then add a _parameterized_ route to the `AppRoutingModule.routes` array that matches the path pattern to the _hero detail_ view.
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.ts" header = "src/app/app-routing.module.ts" region = "detail-route" >
2017-11-06 13:02:18 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
The colon (:) in the `path` indicates that `:id` is a placeholder for a specific hero `id` .
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
At this point, all application routes are in place.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/app-routing.module.ts" region = "routes" header = "src/app/app-routing.module.ts (all routes)" >
2017-03-27 11:08:53 -04:00
< / code-example >
2018-02-22 11:08:07 -05:00
### `DashboardComponent` hero links
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
The `DashboardComponent` hero links do nothing at the moment.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Now that the router has a route to `HeroDetailComponent` ,
fix the dashboard hero links to navigate via the _parameterized_ dashboard route.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-example
path="toh-pt5/src/app/dashboard/dashboard.component.html"
region="click"
2018-10-11 07:29:59 -04:00
header="src/app/dashboard/dashboard.component.html (hero links)">
2017-11-06 13:02:18 -05:00
< / code-example >
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
You're using Angular [interpolation binding ](guide/template-syntax#interpolation ) within the `*ngFor` repeater
to insert the current iteration's `hero.id` into each
2017-11-06 13:02:18 -05:00
[`routerLink` ](#routerlink ).
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
{@a heroes-component-links}
2018-02-22 11:08:07 -05:00
### `HeroesComponent` hero links
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
The hero items in the `HeroesComponent` are `<li>` elements whose click events
are bound to the component's `onSelect()` method.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt4/src/app/heroes/heroes.component.html" region = "list" header = "src/app/heroes/heroes.component.html (list with onSelect)" >
2017-11-06 13:02:18 -05:00
< / code-example >
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
Strip the `<li>` back to just its `*ngFor` ,
wrap the badge and name in an anchor element (`< a > `),
2019-01-31 14:25:30 -05:00
and add a `routerLink` attribute to the anchor that
2017-11-06 13:02:18 -05:00
is the same as in the dashboard template
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/heroes/heroes.component.html" region = "list" header = "src/app/heroes/heroes.component.html (list with links)" >
2017-03-27 11:08:53 -04:00
< / code-example >
2017-11-06 13:02:18 -05:00
You'll have to fix the private stylesheet (`heroes.component.css`) to make
the list look as it did before.
Revised styles are in the [final code review ](#heroescomponent ) at the bottom of this guide.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
#### Remove dead code (optional)
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
While the `HeroesComponent` class still works,
2017-11-06 13:02:18 -05:00
the `onSelect()` method and `selectedHero` property are no longer used.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
It's nice to tidy up and you'll be grateful to yourself later.
Here's the class after pruning away the dead code.
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/heroes/heroes.component.ts" region = "class" header = "src/app/heroes/heroes.component.ts (cleaned up)" linenums = "false" >
2017-03-27 11:08:53 -04:00
< / code-example >
2019-01-31 14:25:30 -05:00
## Routable `HeroDetailComponent`
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Previously, the parent `HeroesComponent` set the `HeroDetailComponent.hero`
property and the `HeroDetailComponent` displayed the hero.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
`HeroesComponent` doesn't do that anymore.
Now the router creates the `HeroDetailComponent` in response to a URL such as `~/detail/11` .
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
The `HeroDetailComponent` needs a new way to obtain the hero-to-display.
This section explains the following:
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
* Get the route that created it
2017-11-06 13:02:18 -05:00
* Extract the `id` from the route
* Acquire the hero with that `id` from the server via the `HeroService`
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Add the following imports:
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/hero-detail/hero-detail.component.ts" region = "added-imports" header = "src/app/hero-detail/hero-detail.component.ts" >
2017-11-06 13:02:18 -05:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
{@a hero-detail-ctor}
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
Inject the `ActivatedRoute` , `HeroService` , and `Location` services
into the constructor, saving their values in private fields:
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/hero-detail/hero-detail.component.ts" header = "toh-pt5/src/app/hero-detail/hero-detail.component.ts" region = "ctor" >
2017-11-06 13:02:18 -05:00
< / code-example >
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
The [`ActivatedRoute` ](api/router/ActivatedRoute ) holds information about the route to this instance of the `HeroDetailComponent` .
2019-01-31 14:25:30 -05:00
This component is interested in the route's parameters extracted from the URL.
The "id" parameter is the `id` of the hero to display.
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
The [`HeroService` ](tutorial/toh-pt4 ) gets hero data from the remote server
2019-01-31 14:25:30 -05:00
and this component will use it to get the hero-to-display.
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
The [`location` ](api/common/Location ) is an Angular service for interacting with the browser.
You'll use it [later ](#goback ) to navigate back to the view that navigated here.
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
### Extract the `id` route parameter
2017-03-30 15:04:18 -04:00
2017-11-06 13:02:18 -05:00
In the `ngOnInit()` [lifecycle hook ](guide/lifecycle-hooks#oninit )
call `getHero()` and define it as follows.
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/hero-detail/hero-detail.component.ts" header = "src/app/hero-detail/hero-detail.component.ts" region = "ngOnInit" >
2017-02-22 13:09:39 -05:00
< / code-example >
2017-11-06 13:02:18 -05:00
The `route.snapshot` is a static image of the route information shortly after the component was created.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
The `paramMap` is a dictionary of route parameter values extracted from the URL.
The `"id"` key returns the `id` of the hero to fetch.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Route parameters are always strings.
The JavaScript (+) operator converts the string to a number,
which is what a hero `id` should be.
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
The browser refreshes and the app crashes with a compiler error.
`HeroService` doesn't have a `getHero()` method.
Add it now.
2017-02-22 13:09:39 -05:00
2018-02-22 11:08:07 -05:00
### Add `HeroService.getHero()`
2017-03-30 15:04:18 -04:00
2019-01-31 14:25:30 -05:00
Open `HeroService` and add the following `getHero()` method with the `id` after the `getHeroes()` method:
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/hero.service.ts" region = "getHero" header = "src/app/hero.service.ts (getHero)" >
2017-02-22 13:09:39 -05:00
< / code-example >
2017-11-06 13:02:18 -05:00
< div class = "alert is-important" >
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
Note the backticks ( ` ) that define a JavaScript
2017-11-06 13:02:18 -05:00
[_template literal_ ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals ) for embedding the `id` .
< / div >
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Like [`getHeroes()` ](tutorial/toh-pt4#observable-heroservice ),
`getHero()` has an asynchronous signature.
It returns a _mock hero_ as an `Observable` , using the RxJS `of()` function.
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
You'll be able to re-implement `getHero()` as a real `Http` request
without having to change the `HeroDetailComponent` that calls it.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
#### Try it
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
The browser refreshes and the app is working again.
You can click a hero in the dashboard or in the heroes list and navigate to that hero's detail view.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
If you paste `localhost:4200/detail/11` in the browser address bar,
2019-04-18 03:39:56 -04:00
the router navigates to the detail view for the hero with `id: 11` , "Dr Nice".
2017-03-31 07:23:16 -04:00
2017-11-06 13:02:18 -05:00
{@a goback}
2017-03-31 07:23:16 -04:00
2017-11-06 13:02:18 -05:00
### Find the way back
2017-03-31 07:23:16 -04:00
2019-01-31 14:25:30 -05:00
By clicking the browser's back button,
2017-11-06 13:02:18 -05:00
you can go back to the hero list or dashboard view,
depending upon which sent you to the detail view.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
It would be nice to have a button on the `HeroDetail` view that can do that.
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Add a *go back* button to the bottom of the component template and bind it
to the component's `goBack()` method.
2017-03-31 07:23:16 -04:00
2019-01-31 14:25:30 -05:00
< code-example path = "toh-pt5/src/app/hero-detail/hero-detail.component.html" region = "back-button" header = "src/app/hero-detail/hero-detail.component.html (back button)" >
2017-11-06 13:02:18 -05:00
< / code-example >
2017-03-31 07:23:16 -04:00
2019-01-31 14:25:30 -05:00
Add a `goBack()` _method_ to the component class that navigates backward one step
2017-11-06 13:02:18 -05:00
in the browser's history stack
using the `Location` service that you [injected previously ](#hero-detail-ctor ).
2017-03-31 07:23:16 -04:00
2018-10-11 07:29:59 -04:00
< code-example path = "toh-pt5/src/app/hero-detail/hero-detail.component.ts" region = "goBack" header = "src/app/hero-detail/hero-detail.component.ts (goBack)" >
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
< / code-example >
2017-03-31 19:57:13 -04:00
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
Refresh the browser and start clicking.
Users can navigate around the app, from the dashboard to hero details and back,
from heroes list to the mini detail to the hero details and back to the heroes again.
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
## Final code review
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
Here are the code files discussed on this page and your app should look like this < live-example > < / live-example > .
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
{@a approutingmodule}
{@a appmodule}
2019-01-31 14:25:30 -05:00
#### `AppRoutingModule`, `AppModule`, and `HeroService`
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
< code-tabs >
2019-01-31 14:25:30 -05:00
< code-pane
header="src/app/app-routing.module.ts"
2017-11-06 13:02:18 -05:00
path="toh-pt5/src/app/app-routing.module.ts">
< / code-pane >
2019-01-31 14:25:30 -05:00
< code-pane
header="src/app/app.module.ts"
2017-11-06 13:02:18 -05:00
path="toh-pt5/src/app/app.module.ts">
< / code-pane >
2019-01-31 14:25:30 -05:00
< code-pane
header="src/app/hero.service.ts"
2018-02-22 11:08:07 -05:00
path="toh-pt5/src/app/hero.service.ts">
< / code-pane >
2017-11-06 13:02:18 -05:00
< / code-tabs >
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
{@a appcomponent}
2019-01-31 14:25:30 -05:00
#### `AppComponent`
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
< code-tabs >
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/app.component.html"
2017-11-06 13:02:18 -05:00
path="toh-pt5/src/app/app.component.html">
< / code-pane >
2017-02-22 13:09:39 -05:00
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/app.component.css"
2017-11-06 13:02:18 -05:00
path="toh-pt5/src/app/app.component.css">
< / code-pane >
< / code-tabs >
2017-02-22 13:09:39 -05:00
2017-11-06 13:02:18 -05:00
{@a dashboardcomponent}
2019-01-31 14:25:30 -05:00
#### `DashboardComponent`
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
< code-tabs >
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/dashboard/dashboard.component.html" path="toh-pt5/src/app/dashboard/dashboard.component.html">
2017-11-06 13:02:18 -05:00
< / code-pane >
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/dashboard/dashboard.component.ts" path="toh-pt5/src/app/dashboard/dashboard.component.ts">
2017-11-06 13:02:18 -05:00
< / code-pane >
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/dashboard/dashboard.component.css" path="toh-pt5/src/app/dashboard/dashboard.component.css">
2017-11-06 13:02:18 -05:00
< / code-pane >
< / code-tabs >
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
{@a heroescomponent}
2019-01-31 14:25:30 -05:00
#### `HeroesComponent`
2018-02-22 11:08:07 -05:00
2017-11-06 13:02:18 -05:00
< code-tabs >
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/heroes/heroes.component.html" path="toh-pt5/src/app/heroes/heroes.component.html">
2017-11-06 13:02:18 -05:00
< / code-pane >
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-pane
header="src/app/heroes/heroes.component.ts"
2017-11-06 13:02:18 -05:00
path="toh-pt5/src/app/heroes/heroes.component.ts">
< / code-pane >
2017-03-31 19:57:13 -04:00
2019-01-31 14:25:30 -05:00
< code-pane
header="src/app/heroes/heroes.component.css"
2017-11-06 13:02:18 -05:00
path="toh-pt5/src/app/heroes/heroes.component.css">
< / code-pane >
< / code-tabs >
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
{@a herodetailcomponent}
2019-01-31 14:25:30 -05:00
#### `HeroDetailComponent`
2017-03-27 11:08:53 -04:00
2017-11-06 13:02:18 -05:00
< code-tabs >
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/hero-detail/hero-detail.component.html" path="toh-pt5/src/app/hero-detail/hero-detail.component.html">
2017-11-06 13:02:18 -05:00
< / code-pane >
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/hero-detail/hero-detail.component.ts" path="toh-pt5/src/app/hero-detail/hero-detail.component.ts">
2017-11-06 13:02:18 -05:00
< / code-pane >
2017-03-27 11:08:53 -04:00
2019-01-31 14:25:30 -05:00
< code-pane
2018-10-11 07:29:59 -04:00
header="src/app/hero-detail/hero-detail.component.css" path="toh-pt5/src/app/hero-detail/hero-detail.component.css">
2017-11-06 13:02:18 -05:00
< / code-pane >
< / code-tabs >
2017-03-31 19:57:13 -04:00
2017-11-06 13:02:18 -05:00
## Summary
2017-02-22 13:09:39 -05:00
2017-03-31 19:57:13 -04:00
* You added the Angular router to navigate among different components.
2017-11-13 21:38:03 -05:00
* You turned the `AppComponent` into a navigation shell with `<a>` links and a `<router-outlet>` .
2019-01-31 14:25:30 -05:00
* You configured the router in an `AppRoutingModule`
2017-11-06 13:02:18 -05:00
* You defined simple routes, a redirect route, and a parameterized route.
* You used the `routerLink` directive in anchor elements.
* You refactored a tightly-coupled master/detail view into a routed detail view.
* You used router link parameters to navigate to the detail view of a user-selected hero.
2017-03-31 19:57:13 -04:00
* You shared the `HeroService` among multiple components.