# Routing There are new requirements for the Tour of Heroes app: * Add a *Dashboard* view. * 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. When you’re done, users will be able to navigate the app like this:
View navigations
## Add the `AppRoutingModule` In Angular, the best practice is to load and configure the router in a separate, top-level module that is dedicated to routing and imported by the root `AppModule`. By convention, the module class name is `AppRoutingModule` and it belongs in the `app-routing.module.ts` in the `src/app` folder. Use the CLI to generate it. ng generate module app-routing --flat --module=app
`--flat` puts the file in `src/app` instead of its own folder.
`--module=app` tells the CLI to register it in the `imports` array of the `AppModule`.
The generated file looks like this: You generally don't declare components in a routing module so you can delete the `@NgModule.declarations` array and delete `CommonModule` references too. You'll configure the router with `Routes` in the `RouterModule` so import those two symbols from the `@angular/router` library. Add an `@NgModule.exports` array with `RouterModule` in it. Exporting `RouterModule` makes router directives available for use in the `AppModule` components that will need them. `AppRoutingModule` looks like this now: ### Add routes *Routes* tell the router which view to display when a user clicks a link or pastes a URL into the browser address bar. A typical Angular `Route` has two properties: 1. `path`: a string that matches the URL in the browser address bar. 1. `component`: the component that the router should create when navigating to this route. You intend to navigate to the `HeroesComponent` when the URL is something like `localhost:4200/heroes`. Import the `HeroesComponent` so you can reference it in a `Route`. Then define an array of routes with a single `route` to that component. Once you've finished setting up, the router will match that URL to `path: 'heroes'` and display the `HeroesComponent`. ### _RouterModule.forRoot()_ You first must initialize the router and start it listening for browser location changes. Add `RouterModule` to the `@NgModule.imports` array and configure it with the `routes` in one step by calling `RouterModule.forRoot()` _within_ the `imports` array, like this:
The method is called `forRoot()` because you configure the router at the application's root level. The `forRoot()` method supplies the service providers and directives needed for routing, and performs the initial navigation based on the current browser URL.
## Add _RouterOutlet_ Open the `AppComponent` template replace the `` element with a `` element. You removed `` because you will only display the `HeroesComponent` when the user navigates to it. The `` tells the router where to display routed views.
The `RouterOutlet` is one of the router directives that became available to the `AppComponent` because `AppModule` imports `AppRoutingModule` which exported `RouterModule`.
#### Try it You should still be running with this CLI command. ng serve The browser should refresh and display the app title but not the list of heroes. Look at the browser's address bar. The URL ends in `/`. The route path to `HeroesComponent` is `/heroes`. Append `/heroes` to the URL in the browser address bar. You should see the familiar heroes master/detail view. {@a routerlink} ## Add a navigation link (`routerLink`) Users shouldn't have to paste a route URL into the address bar. They should be able to click a link to navigate. Add a `