| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | extends ../../../ts/_cache/tutorial/toh-pt5 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block includes | 
					
						
							|  |  |  |   include ../_util-fns | 
					
						
							|  |  |  |   - var _appRoutingTsVsAppComp = 'AppComponent' | 
					
						
							|  |  |  |   - var _declsVsDirectives = 'directives' | 
					
						
							|  |  |  |   - var _RoutesVsAtRouteConfig = '@RouteConfig' | 
					
						
							|  |  |  |   - var _RouterModuleVsRouterDirectives = 'ROUTER_DIRECTIVES' | 
					
						
							|  |  |  |   - var _redirectTo = 'useAsDefault' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block intro-file-tree | 
					
						
							|  |  |  |   .filetree | 
					
						
							| 
									
										
										
										
											2016-07-01 11:05:45 -07:00
										 |  |  |     .file angular2_tour_of_heroes | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  |     .children | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |       .file lib | 
					
						
							|  |  |  |       .children | 
					
						
							|  |  |  |         .file app_component.dart | 
					
						
							|  |  |  |         .file hero.dart | 
					
						
							|  |  |  |         .file hero_detail_component.dart | 
					
						
							|  |  |  |         .file hero_service.dart | 
					
						
							|  |  |  |         .file mock_heroes.dart | 
					
						
							|  |  |  |       .file web | 
					
						
							|  |  |  |       .children | 
					
						
							|  |  |  |         .file index.html | 
					
						
							|  |  |  |         .file main.dart | 
					
						
							|  |  |  |         .file styles.css | 
					
						
							|  |  |  |       .file pubspec.yaml | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block keep-app-running | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     ### Keep the app compiling and running | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     Open a terminal/console window. | 
					
						
							|  |  |  |     Start the Dart compiler, watch for changes, and start our server by entering the command: | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |   code-example(language="bash"). | 
					
						
							|  |  |  |     pub serve | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block app-comp-v1 | 
					
						
							|  |  |  |   +makeExcerpt('lib/app_component_1.dart (v1)', '') | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block angular-router | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     The Angular router is a combination of multiple services | 
					
						
							|  |  |  |     (`ROUTER_PROVIDERS`), multiple directives (`ROUTER_DIRECTIVES`), and a | 
					
						
							|  |  |  |     configuration annotation (`RouteConfig`). We'll get them all by importing | 
					
						
							|  |  |  |     the router library: | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |   +makeExcerpt('app/app.component.ts (router imports)', 'import-router') | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     ### Make the router available | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     Not all apps need routing, which is why the Angular *Component Router* is | 
					
						
							|  |  |  |     in a separate, optional library module. | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     Like for any service, we make router services available to the application | 
					
						
							|  |  |  |     by adding them to the `providers` list.  Let's update the `directives` and | 
					
						
							|  |  |  |     `providers` lists to include the router assets: | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |   +makeExcerpt('app/app.component.ts (excerpt)', 'directives-and-providers') | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     Notice that we also removed the `HeroesComponent` from the `directives` list. | 
					
						
							|  |  |  |     `AppComponent` no longer shows heroes; that will be the router's job. | 
					
						
							|  |  |  |     We'll soon remove `<my-heroes>` from the template too. | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block router-config-intro | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     ### Configure routes and add the router | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     The `AppComponent` doesn't have a router yet. We'll use the `@RouteConfig` | 
					
						
							|  |  |  |     annotation to simultaneously: | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     - Assign a router to the component | 
					
						
							|  |  |  |     - Configure that router with *routes* | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block routerLink | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     Notice the `[routerLink]` binding in the anchor tag. | 
					
						
							|  |  |  |     We bind the `RouterLink` directive (another of the `ROUTER_DIRECTIVES`) to a list | 
					
						
							|  |  |  |     that tells the router where to navigate when the user clicks the link. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     We define a *routing instruction* with a *link parameters list*. | 
					
						
							|  |  |  |     The list only has one element in our little sample, the quoted ***name* of the route** to follow. | 
					
						
							|  |  |  |     Looking back at the route configuration, we confirm that `'Heroes'` is the name of the route to the `HeroesComponent`. | 
					
						
							|  |  |  |   .l-sub-section | 
					
						
							|  |  |  |     :marked | 
					
						
							|  |  |  |       Learn about the *link parameters list* | 
					
						
							|  |  |  |       in the [Routing](../guide/router.html#link-parameters-array) chapter. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block redirect-vs-use-as-default | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     We don't need a route definition for that. Instead, | 
					
						
							|  |  |  |     we add `useAsDefault: true` to the dashboard *route definition* and the | 
					
						
							|  |  |  |     router will display the dashboard when the browser URL doesn't match an existing route. | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block templateUrl-path-resolution | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     The value of `templateUrl` can be an [asset][] in this package or another | 
					
						
							|  |  |  |     package. To use an asset in another package, use a full package reference, | 
					
						
							|  |  |  |     such as `'package:some_other_package/dashboard_component.html'`. | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     [asset]: https://www.dartlang.org/tools/pub/glossary#asset | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block route-params | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     We will no longer receive the hero in a parent component property binding. | 
					
						
							|  |  |  |     The new `HeroDetailComponent` should take the `id` parameter from the router's | 
					
						
							|  |  |  |     `RouteParams` service and use the `HeroService` to fetch the hero with that `id`. | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block ngOnInit | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     Inside the `ngOnInit` lifecycle hook, extract the `id` parameter value from the `RouteParams` service | 
					
						
							|  |  |  |     and use the `HeroService` to fetch the hero with that `id`. | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block extract-id | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     Notice how we extract the `id` by calling the `RouteParams.get` method. | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block heroes-component-cleanup | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |     Because the template for `HeroesComponent` no longer uses `HeroDetailComponent` | 
					
						
							|  |  |  |     directly — instead using the router to _navigate_ to it — we can | 
					
						
							|  |  |  |     drop the `directives` argument from `@Component` and remove the unused hero detail | 
					
						
							|  |  |  |     import. The revised `@Component` looks like this: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block css-files | 
					
						
							|  |  |  |   +makeTabs( | 
					
						
							|  |  |  |     `toh-5/dart/lib/hero_detail_component.css, | 
					
						
							|  |  |  |     toh-5/dart/lib/dashboard_component.css`, | 
					
						
							|  |  |  |     null, | 
					
						
							|  |  |  |     `lib/hero_detail_component.css, | 
					
						
							|  |  |  |     lib/dashboard_component.css`) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | block router-link-active | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |   :marked | 
					
						
							|  |  |  |     **The *router-link-active* class** | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |     The Angular Router adds the `router-link-active` class to the HTML navigation element | 
					
						
							|  |  |  |     whose route matches the active route. All we have to do is define the style for it. Sweet! | 
					
						
							| 
									
										
										
										
											2016-05-19 08:03:23 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  | block file-tree-end | 
					
						
							|  |  |  |   .filetree | 
					
						
							|  |  |  |     .file angular2_tour_of_heroes | 
					
						
							| 
									
										
										
										
											2016-06-15 07:46:26 -07:00
										 |  |  |     .children | 
					
						
							| 
									
										
										
										
											2016-08-17 13:31:40 -07:00
										 |  |  |       .file lib | 
					
						
							|  |  |  |       .children | 
					
						
							|  |  |  |         .file app_component.css | 
					
						
							|  |  |  |         .file app_component.dart | 
					
						
							|  |  |  |         .file dashboard_component.css | 
					
						
							|  |  |  |         .file dashboard_component.dart | 
					
						
							|  |  |  |         .file dashboard_component.html | 
					
						
							|  |  |  |         .file hero.dart | 
					
						
							|  |  |  |         .file hero_detail_component.css | 
					
						
							|  |  |  |         .file hero_detail_component.dart | 
					
						
							|  |  |  |         .file hero_detail_component.html | 
					
						
							|  |  |  |         .file hero_service.dart | 
					
						
							|  |  |  |         .file heroes_component.css | 
					
						
							|  |  |  |         .file heroes_component.dart | 
					
						
							|  |  |  |         .file heroes_component.html | 
					
						
							|  |  |  |         .file mock_heroes.dart | 
					
						
							|  |  |  |       .file web | 
					
						
							|  |  |  |       .children | 
					
						
							|  |  |  |         .file index.html | 
					
						
							|  |  |  |         .file main.dart | 
					
						
							|  |  |  |         .file styles.css | 
					
						
							|  |  |  |       .file pubspec.yaml |