include ../../../../_includes/_util-fns :marked # Routing Around the App Our Tour of Heroes is a single view, but we have new requirements to create other views, such as a dashboard, and navigate between them. We’ll add a dashboard component and use Angular’s router to handle navigation between views. We have another requirement to allow selecting a hero from either the dashboard or the heroes view and route directly to the hero details. We’ll need to learn about and use route parameters to tackle this. When we’re done, users will be able to navigate the app like this: figure.image-display img(src='/resources/images/devguide/toh/nav-diagram.png' alt="View navigations") :marked Finally, we’ll want to filter and format data in our app using Angular’s Pipes. ## Reviewing Where We Left Off Let’s verify that we have the following structure after adding our hero service and hero detail component in the previous chapter: .filetree .file angular2-tour-of-heroes .children .file node_modules .file app .children .file app.component.ts .file boot.ts .file hero.ts .file hero-detail.component.ts .file hero.service.ts .file mock-heroes.ts .file index.html .file package.json .file tsconfig.json :marked ### Keep the app transpiling and running We want to start the TypeScript compiler, have it watch for changes, and start our server. We'll do this by typing code-example(format="." language="bash"). npm start :marked ## Flying Overhead Before we dash into routing for our Tour of Heroes, let’s fly over what we’re going to need to do. Since we’ll want to be routing to entirely different views, we’ll want to separate a few of the components and their template content. We’re also going to be adding two entirely new components. The first for our new requirement, the dashboard. The second component will be to host our menus with routing links and configuration. Here is our checklist of what we’ll need to tackle 1. Move the heroes from AppComponent to the more aptly named HeroesComponent 1. Create a new AppComponent that will host the menu links and routing 1. Create the DashboardComponent to show our top heroes 1. Create a class to handle routing configuration We’ll tackle these by separating the components, creating our new components, and adding routing so we can navigate around our Tour of Heroes. ## Separating the Components We’ll want a component to host the menu links for the heroes and the dashboard. This will be the first component that our app loads. But currently our app loads `AppComponent` first, which has our list of heroes. It’s time to separate our components so we have a component that hosts our menu links and a component that lists our heroes. We’ll call these `AppComponent` and `HeroesComponent`. ### Creating the HeroesComponent Since we have an `AppComponent` that lists heroes, let’s start by renaming `app.component.ts` to `hero.component.ts`. Then we’ll rename the component from `AppComponent` to `HeroComponent` and we’ll rename the selector to `my-heroes`. code-example. selector: 'my-heroes', :marked Finally, we’ll export the `HeroesComponent` as we will want to use it from another module when we define our routing. code-example. export class HeroesComponent { :marked ## Creating the New AppComponent Our app needs a menu and a place to show the dashboard and heroes views. This is effectively the shell for our app. We’ll create a new file named `app.component.ts` and create our new `AppComponent` inside of the file. This will be the first component we load in our app. It will host the menu links, when we create them. We assign our `AppComponent` a selector of `my-app`. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts') :marked `AppComponent` is the entry point of our app. This makes it the ideal place to bootstrap our app, which is why we pass in the `AppComponent` and the shared `HeroService` that all many of our components will use. We export our `AppComponent` as we’ll want to refer to it from our bootstrapping process. ## Bootstrapping the Tour of Heroes The start-up of an app is also known as bootstrapping. We are currently bootstrapping in our `HeroesComponent`, which no longer makes sense. So let’s change that and separate this startup logic. Let’s move the bootstrapping logic into a new file. We’ll create a new file named `boot.ts` in the `app` folder. Let’s add the following lines to `boot.ts`: +makeExample('toh-4/ts-snippets/app/bootstrap.pt4.ts') :marked The bootstrap function accepts as its first parameter, the first component that the app will use. Now let’s do a little cleanup work. Let’s remove the bootstrap logic and remove `bootstrap` from the import statement in the `heroes.component.ts`. We had already exported our `AppComponent`, which is why we can now import it and bootstrap it here. We pass in the shared `HeroService` that many of our components will use, so it will be ready when we need it. Now let’s tell our module loader to start by loading our `bootstrap` module. We’ll do this in our`index.html` file +makeExample('toh-4/ts-snippets/app/index.pt4.html','bootstrap') :marked ### Viewing our Progress Let’s add a title for our app which we’ll bind to a `title` property on our component. We’ll set the title to “Tour of Heroes”. +makeExample('toh-4/ts-snippets/app/index.pt4.html','title') :marked Our title now belongs in the `AppComponent`, but it also still exists in `HeroesComponent`. So let’s tidy up by removing the `title` from the `HeroesComponent` class and template. When we view our app in the browser we should now only see our title of “Tour of Heroes”. But where is the rest of our app? We haven’t shown it yet! Our app’s entry point is the `bootstrap` module which loads the `AppComponent`. `AppComponent` in turn only shows a title. Our next step is to configure the menu links and routes that will show our views. ## Adding the Router to our App The Angular router is a separate and distinct module that we can include as needed. Well, our Tour of Heroes app needs routing, so let’s add it! ### Including the Router We add a script tag referencing the router code. We’ll make sure this comes after the angular script reference. Then let’s set our base href to `/src/` since that is where our source code is located. Our `index.html`’s head section should nw look like this: +makeExample('toh-4/ts-snippets/app/index.pt4.html','head') :marked Now we’ll be loading the Angular router! ### Configuring Routes We want to display a menu that has links to a dashboard and to our list of heroes. Let’s configure the first route to show our `HeroesComponent`. Our app will have a few routes and we may want to use them in more than one module. Let’s create a file named `route.config.ts` to host our routes. #### Defining Routes We want to show our heroes list. So let’s define our first route to show our `HeroesComponent` template. +makeExample('toh-4/ts-snippets/app/route.config.pt4.ts','first-route') :marked Our route needs a path. This is what will show up in the address bar of the browser. Our path for our `HeroesComponent` will be `/`. The `component` property identifies the component we will load when we go to this route. In this case we want to load the `HeroesComponent`. The `as` property is what the route is known as. In other words, it is the name of the route. We set these three properties in an object and export that object in an array named `APP_ROUTES`. Right now we have one route. But as we add more routes, this technique of hosting them in a `route.config.ts` file and exporting an array of them will make it easier to manage our routes. #### The RouteConfig Decorator Now that we have defined a route, we need to tell Angular where to find it. Let’s go to our `AppComponent` and add the `RouteConfig` decorator the class. We’ll need to import the `RouteConfig` decorator from Angular’s router module, too. Now we import the `APP_ROUTES`array of routes that we just created. We’ll pass these into the `RouteConfig` decorator. +makeExample('toh-4/ts-snippets/app/index.pt4.html','routes-title') :marked Our app now has its first route, but we need a place to show the heroes view. ### Showing the View with the Router-Outlet Angular knows we have a route when we navigate to `/heroes`. But where does the view show in the HTML? We haven’t told our app how to do that yet! Let’s add the `` directive to the template of `AppComponent`. The `` is a placeholder that the router uses to place the views. When we go to the `/heroes` route, the router will show the `HeroesComponent` template where we place the ``. +makeExample('toh-4/ts-snippets/app/index.pt4.html','router-outlet') :marked When need to declare to the `AppComponent` that we are using the `router-outlet` directive. To do this we’ll import a special `ROUTER_DIRECTIVES` array of router specific directives. ``` import {RouteConfig, ROUTER_DIRECTIVES} from 'angular2/router'; ``` Then we’ll declare them to the component in the `@Component` decorator’s `directives` property. ``` directives: [ROUTER_DIRECTIVES] ``` Let’s go view our app in the browser and see where we are. Uh oh, we see an error in the developer console. code-example(language="html"). EXCEPTION: No provider for Router! (RouterOutlet -> Router) :marked Angular is warning us that we are using the router and the `router-outlet` but we did not inject the router’s provider. We can fix this by injecting this in the bootstrap module. First we’ll import the `ROUTER_PROVIDERS` from the router module. ``` import {ROUTER_PROVIDERS} from 'angular2/router'; ``` Then we’ll pass the `ROUTER_PROVIDERS` to the `bootstrap` method. ``` bootstrap(AppComponent, [ROUTER_PROVIDERS, HeroService]); ``` Now when we view our app in the browser we see our heroes list! ## Creating Navigation Links Okay, our Tour of Heroes is not quite where we want it yet. We’ve created the `AppComponent` which hosts the routing and we move the heroes list to the `HeroesComponent`. But to fulfill our requirements we need to create a dashboard and add a way to navigate between the different views. Let’s continue by adding the new dashboard component and the navigation links. ### Empty Dashboard Routing makes a lot more sense once we have multiple views. We have a heroes view to show but now we need to create our dashboard view. Let’s create the `DashboardComponent`so we can finish creating the navigation between the components. Let’s create a super simple dashboard component. +makeExample('toh-4/ts-snippets/app/index.pt4.html','simple-dashboard-component') :marked We’ll come back to the dashboard once we complete routing. For now this will do nicely to help us make sure we can navigate between the `HeroesComponent` and the `DashboardComponent`. ### Configuring the Dashboard’s Routes Now that we have a component for our dashboard, let’s go configure a route that will take us there. We’ll open `route.config.ts` and add another route for the dashboard. Our dashboard should be the first thing we see when load our app. So the dashboard route will be the default route of `/` while our heroes will now be accessible via the path `/heroes`. We’ll also import the `DashboarComponent` so we can route to it with the dashboard route. And we’ll add the dashboard route to the `APP_ROUTES` export. Our `route.config.ts` should now look like the following code: +makeExample('toh-4/ts-snippets/app/route.config.pt4.ts','dashboard-route') :marked Now we two components we can route between and we have defined the routes for both components. Next up, we’ll add navigation links to route between them. ### Navigation Links Let’s add the navigation links to the`AppComponent`’s template. The Angular router uses a special `router-link` directive to navigate to the routes we defined. We can think of these as links that will navigate to another component using the router. We also add a title for our app which we’ll bind to a `title` property on our component. +makeExample('toh-4/ts-snippets/app/index.pt4.html','router-link') :marked The `router-link` is not set to url. We bound the `router-link` to the routes that we specified in the `as` property of our `@RouteConfig`. The `as` is the key that we use to reference the routes. #### Reusable Routing Config We just hard-coded the routes that the `router-link` properties are bound. We can do better. We recall that we previously created route configuration in `route.config.ts`. We import its `Routes`. +makeExample('toh-4/ts-snippets/app/index.pt4.html','import-app-routes') :marked And then we use it to initialize a `routes` property on our `AppComponent`. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','initialize-routes-property') :marked Then we can simply reference the routes as shown below using our route configuration variables, such as `routes.heroes.as`. +makeExample('toh-4/ts-snippets/app/index.pt4.html','router-link') :marked When we view our app in the browser we are brought directly to our dashboard. We can navigate between the dashboard and the heroes til our hearts are content. ## Adding the Top Heroes to Our Dashboard Our dashboard view is a bit bland as it only contains a title. Let’s spice it up by adding the top 4 heroes at a glance. ### Top Heroes Template Content Let’s add the template to our dashboard to show the top four heroes. We’ll use the `ngFor` directive to iterate over a list of heroes (which we have not retrieved yet) and display them. We’ll use `
` elements as we’re going to custom style them. +makeExample('toh-4/ts-snippets/app/index.pt4.html','ng-for') :marked We’ve been down this road before. We are using the `ngFor` directive, so we have to declare it in the component. Let’s do that now by first importing `CORE_DIRECTIVES`. (Remember that `CORE_DIRECTIVES` is a convenience array containing the most common directives such as `ngFor`.) ``` import {Component, CORE_DIRECTIVES} from 'angular2/angular2'; ``` Then we declare the `CORE_DIRECTIVES` to the component. ``` directives: [CORE_DIRECTIVES] ``` ### Using the Shared HeroService We just iterated over a list of heroes, but we don’t have any heroes in the `DashboardComponent`. We do have a `HeroService` that provides heroes. In fact, we already used this service in the `HeroComponent`. Let’s re-use this same service for the `DashboardComponent` to get a list of heroes. We’ll create a `heroes` property in our `DashboardComponent`. ``` public heroes: Hero[]; ``` And we import the `Hero` ``` import {Hero} from './hero'; ``` #### Injecting a Service We’ll be needing the `HeroService`, so let’s import it and inject it into our `DashboardComponent`. Here we import it. ``` import {HeroService} from './hero.service'; ``` And here we inject it into our component’s constructor. ``` constructor(private _heroService: HeroService) { } ``` #### Getting the Heroes on Initialization We want our heroes to be loaded when the component is initialized, just like we did in the `HeroesComponent`. We’ll tackle this the same way, by using the onInit Lifecycle hook. We can implement the `OnInit` interface and code the `onInit` method. ``` export class DashboardComponent implements OnInit { ``` Here we implement the `onInit` method to get our heroes, again just like we did for the `HeroesComponent`. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','oninit') :marked Notice we did not have to know how to get the heroes, we just needed to know the method to call from the `HeroService`. This is an advantage of using shared services. When we view our app in the browser we see the dashboard light up with all of our heroes. This isn’t exactly what we want so let’s trim that down to the top four heroes. ### Slicing with Pipes Our requirement is to show the top four heroes. If only we had something that would automatically get a subset of the data. Well, we do! They are called Pipes. Angular has various built-in pipes that make formatting and filtering data easy. We’ll take advantage of a pipe named `slice` to get a slice of our heroes array. ```
``` After the `ngFor` we added a pipe character and then the `slice` pipe. We tell the `slice` pipe to start at the 0th item in the array and get four items. .l-sub-section :marked Learn more about Pipes in the chapter [Pipes](../guide/pipes.html) :marked When we view our app we now see the first four heroes are displayed. ### Heroes with Style Our creative designers have added a requirement that the dashboard should show the heroes in a row of rectangles. We’ve written some CSS to achieve this along with some simple media queries to achieve responsive design. We could put the CSS in the component, but there are over 30 lines of CSS and it would get crowded fast. Most editors make it easier to code CSS in a *.css file too. Fortunately, we can separate the styles into their own file and reference them. #### Adding the Dashboard’s CSS File Let’s create a file to hold the `DashboardComponent`’s CSS. We’ll name it `dashboard.component.css` and put it in the `app` folder. Now let’s add the following CSS to the file. +makeExample('toh-4/ts-snippets/app/index.pt4.html','css') :marked We need to reference the file from our component so the styles will be applied properly. Let’s add this reference to the component’s `styleUrls` property. ``` styleUrls: ['app/dashboard.component.css'], ``` The `styleUrls` property is an array, which we might guess suggests that we can add multiple styles from different locations. And we would be right! In this case we have one file, but we could add more for our component if needed. #### Template Urls While we are at it, let’s move our HTML for the `DashboardComponent` to its own file. We’ll create a file named `dashboard.component.html` in the `app` folder and move the HTML there. We need to reference the the template, so let’s change our component’s `template` property to `templateUrl` and set it to the location of the file. ``` template: 'app/dashboard.component.html', ``` Notice we are now using single quotes and not the back-ticks since we only have the need for a single line string. #### Applying the Styles to Our Template Now that we have some style, let’s take advantage of it by applying it to our template. Our template should now look like this: +makeExample('toh-4/ts-snippets/app/index.pt4.html','template-styled') :marked When we view our app in the browser it now shows our dashboard with our four top heroes in a row. ## Styling the Navigation Links Our creative design team requested that the navigation links also be styled. Let’s make the navigation links look more like selectable buttons. ### Defining Styles for Navigation Links Let’s add the following styles to our `AppComponent`’s `styles` property. We’ll define some classes named `router-link` that style the default, active, visited and hover selectors. The active selector changes the color of the link to make it easy to identify which link is selected. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','styles') :marked This time we define the styles in our component because there are only a few of them. ### Applying Styles to the Navigation Links Now that we have styles for our navigation links, let’s apply them in the `AppComponent` template. When we are done, our component will look like the following: +makeExample('toh-4/ts-snippets/app/index.pt4.html','styled-nav-links') :marked #### Where Did the Active Route Come From? The Angular Router makes it easy for us to style our active navigation link. The `router-link-active` class is automatically added to the Router’s active route for us. So all we have to do is define the style for it. Sweet! When we view our app in the browser we see the navigation links are now styled, as are our top heroes! figure.image-display img(src='/resources/images/devguide/toh/dashboard-top-heroes.png' alt="View navigations") :marked ## Add Route to Hero Details We can navigate between the dashboard and the heroes view, but we have a requirement from our users to be able to select a hero from either of those views and go directly to the selected hero’s details. Let’s configure a route to go directly to the `HeroDetailComponent` passing the hero’s id as a parameter. ### Configuring a Route with a Parameter We’ve already added a few routes to the `routes.config.ts` file, so it’s natural that we’d start there to add the route to go to the `HeroDetailComponent`. Let’s start by adding the import statement for the component. +makeExample('toh-4/ts-snippets/app/route.config.pt4.ts','route-parameter-import') :marked Now we add a route for the details to the `Routes` object. +makeExample('toh-4/ts-snippets/app/route.config.pt4.ts','route-parameter-detail') :marked The route will lead to the `HeroDetailComponent`, passing along the value for the hero’s id. The routing configuration identifies parameters as parts of the path that are prefixed with a `:` such as `:id`. ### Receiving a Parameter We want to navigate to the `HeroDetailComponent`, so let’s modify it to accept the `id` parameter. We import the `RouteParams` so we can access the parameter. We assign our `AppComponent` a selector of `my-app`. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','import-params') :marked Now we inject the `RouteParams` into the `HeroDetailComponent` constructor. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','inject-routeparams') :marked We want to immediately access the parameter, so let’s implement the `OnInit` interface and its `onInit` method. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','access-params') :marked And let’s not forget to import `OnInit`. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','import-onit') :marked Using the `onInit`method, we can grab the parameter as soon as the component initializes. We’ll access the parameter by name and later we’ll get the hero by its `id`. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','onit-id-param') :marked Our `HeroDetailComponent` is already used in the `HeroesComponent`. When we select a hero from the list we are passing the hero object from the list to the `HeroDetailComponent`. We want this component to support that functionality or be able to accept the hero’s id. Let’s revise the logic in the `onInit` to handle this. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','onit-hero-id') :marked Our component will first check if it has a hero. If it doesn’t it will then check for the routing parameter so it can get the hero. .l-sub-section :marked Learn more about RouteParams in the chapter [Router](../guide/router.html) :marked Getting the Hero When we pass the id to the `HeroDetailComponent` we need to go get the hero from our `HeroService`. Let’s import the `HeroService` so we can use it to get our hero. +makeExample('toh-4/ts-snippets/app/bootstrap.pt4.ts','import-hero-service') :marked And then we inject the `HeroService` into the constructor. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','inject-hero-service') :marked We then stub out the call to the `HeroService` to get the hero by the hero’s id. But wait a second, we don’t have a way to get the hero by id … yet. Our `HeroService` is the right place to get a single hero. We’ll create a method named `getHero` that will accept a parameter, find the hero, and return the hero in a promise. We add this method to the `HeroService`. +makeExample('toh-4/ts-snippets/app/hero.service.pt4.ts','get-hero-method') :marked Then we go back to our `HeroDetailComponent` and we can call the `getHero` method. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','onit-hero-method') :marked We grab the hero and set it to the local `hero` property. Now we have everything in place to receive the parameter. ### Select a Hero on the Dashboard When a user selects a hero in the dashboard, we want to route to the details. Let’s open our dashboard’s template and add a click event to each hero in the template. +makeExample('toh-4/ts-snippets/app/index.pt4.html','select-hero-click-event') :marked The click event will call the `gotoDetail` method in the `DashboardComponent`. We don’t have that method yet, so let’s create it. We’ll want to use the router to navigate to the details route we created. So we have to import the router, and while we are at it, we’ll import the `Routes` object we created that describe our routes. +makeExample('toh-4/ts-snippets/app/bootstrap.pt4.ts','import-router') :marked Now we can write our method to navigate to the route and pass the parameter. We’ll use the router’s `navigate` method and pass an array that has 2 parameters. The first is the name of the route (the `as` property in the `RouteConfig`). The second is an object with the parameters and values. +makeExample('toh-4/ts-snippets/app/route.config.pt4.ts','router-navigate-method') :marked Now when we view our app in the browser and select a hero from the dashboard, we go directly to the hero’s details! .l-sub-section :marked Learn more about RouteParams in the chapter [Router](../guide/router.html) :marked ### Select a Hero on the HeroesComponent When a user selects a hero in the dashboard, we go to the details. But we also want this to happen from the `HeroesComponent`. Let’s add the same changes to the `HeroesComponent` that we made to the dashboard. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','select-hero') :marked The requirement here is to show the hero when selected and allow the user to the details via a button. So when a user selects a hero we want to show the hero’s name and provide a button to navigate to the details. Let’s open the `HeroesComponent`, remove the `my-hero-detail` component, and change the template to display the hero’s name instead. +makeExample('toh-4/ts-snippets/app/index.pt4.html','display-hero-name') :marked We also added a button with a click event that will call our `gotoDetail` method in our `HeroesComponent`. Notice we also used the `uppercase` pipe to format the selected hero’s name. Pipes are extremely helpful at formatting and filtering. .l-sub-section :marked Learn more about Pipes in the chapter [Pipes](../guide/pipes.html) :marked When we view the app in our browser we can now navigate from the dashboard or the heroes component directly to the selected hero’s details! ### Cleaning Up Templates and Styles We’ve added a lot of HTML and CSS in our template and styles, respectively, in the `HeroesComponent`. Let’s move each of these to their own files. We move the HTML for the `HeroesComponent` template to `heroes.component.html`. Then we reference the file in the component’s `templateUrl` property. Now our `HeroesComponent` looks much cleaner and easier to maintain since our template and styles are in another file. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','reference-heroes-component') :marked We’ll also move the HTML out of the `HeroDetailComponent` and into its own file named `hero-detail.component.html`. Then we reference the file from the `templateUrl` property. +makeExample('toh-4/ts-snippets/app/app.component.pt4.ts','reference-hero-detail-component') :marked ### Adding Styles to the App When we add styles to a component we are making it easier to package everything a component needs together. The HTML, the CSS, and the code are all together in one convenient place. However we can also add styles at an app level outside of a component. Our designers just gave us a few more basic styles to apply to our entire app. Let’s add some CSS in a file `styles.css` to the `src` folder to style the app’s basic elements. +makeExample('toh-4/ts-snippets/app/index.pt4.html','basic-styles') :marked And let’s reference the stylesheet from the `index.html`. +makeExample('toh-4/ts-snippets/app/index.pt4.html','stylesheet') :marked When we view our app in the browser we can see everything still works as expected! ### Reviewing the App Structure Let’s verify that we have the following structure after all of our good refactoring in this chapter: .filetree .file angular2-tour-of-heroes .children .file node_modules .file app .children .file app.component.ts .file boot.ts .file dashboard.component.css .file dashboard.component.html .file dashboard.component.ts .file hero.ts .file hero-detail.component.html .file hero-detail.component.ts .file hero.service.ts .file heroes.component.css .file heroes.component.html .file heroes.component.ts .file mock-heroes.ts .file index.html .file package.json .file styles.css .file tsconfig.json :marked .l-main-section :marked ## Recap ### The Road We’ve Travelled Let’s take stock in what we’ve built. - We added the router to navigate between different components and their templates - We learned how to create router links to represent navigation menu items - We extended a component to either accept a hero as input or accept a router parameter to get the hero - We extended our shared service by adding a new method to it - We added the `slice` pipe to filter the top heroes, and the `uppercase` pipe to format data ### The Road Ahead Our Tour of Heroes has grown to reuse services, share components, route between components and their templates, and filter and format data with pipes. We have many of the foundations to build an application. In the next chapter we’ll explore how to replace our mock data with real data using http.