@@ -644,17 +644,67 @@ block extract-id
644
644
Refresh the browser and select a hero from the dashboard; the app should navigate directly to that hero’s details.
645
645
646
646
.l-main-section
647
+ :marked
648
+ ## Refactor routes to a _Routing Module_
649
+
650
+ Almost 20 lines of `AppModule` are devoted to configuring four routes.
651
+ Most application have many more routes and they [add guard services](../guide/router.html#guards)
652
+ to protect against unwanted or unauthorized navigations.
653
+ Routing considerations could quickly dominate this module and obscure its primary purpose which is to
654
+ establish key facts about the entire app for the Angular compiler.
655
+
656
+ We should refactor the routing configuration into its own class.
657
+ What kind of class?
658
+ The current `RouterModule.forRoot()` produces an Angular `ModuleWithProviders` which suggests that a
659
+ class dedicated to routing should be some kind of module.
660
+ It should be a [_Routing Module_](../guide/router.htm#routing-module).
661
+
662
+ By convention the name of a _Routing Module_ contains the word "Routing" and
663
+ aligns with the name of the module that declares the components navigated to".
664
+
665
+ Create an `app-routing.module.ts` file as a sibling to `app.module.ts`. Give it the following contents extracted from the `AppModule` class:
666
+
667
+ + makeExample('app/app-routing.module.ts' )
668
+ :marked
669
+ Noteworthy points, typical of _Routing Modules_:
670
+ * Pull the routes into a variable. You might export it in future and it clarifies the _Routing Module_ pattern.
671
+
672
+ * Add `RouterModule.forRoot(routes)` to `imports`.
673
+
674
+ * Add `RouterModule` to `exports` so that the components in the companion module have access to Router declarables
675
+ such as `RouterLink` and `RouterOutlet`.
676
+
677
+ * No `declarations`! Declarations are the responsibility of the companion module.
678
+
679
+ * Add module `providers` for guard services if you have them; there are none in this example.
680
+
681
+ ### Update _AppModule_
682
+
683
+ Now delete the routing configuration from `AppModule` and import the `AppRoutingModule`
684
+ (_both_ with an ES `import` statement _and_ by adding it to the `NgModule.imports` list).
685
+
686
+ Here is the revised `AppModule`, compared to its pre-refactor state:
687
+ + makeTabs(
688
+ ` toh-5/ts/app/app.module.ts, toh-5/ts/app/app.module.3.ts` ,
689
+ null ,
690
+ ` app/app.module.ts (after), app/app.module.ts (before)` )
691
+ :marked
692
+ It's simpler and focused on indentifying the key pieces of the application.
693
+ .l-main-section
647
694
:marked
648
695
## Select a Hero in the *HeroesComponent*
649
696
697
+ Earlier we added the ability to select a hero from the dashboard.
650
698
We'll do something similar in the `HeroesComponent`.
651
699
652
- That component's current template exhibits a "master/detail" style with the list of heroes
700
+ The `HeroesComponent` template exhibits a "master/detail" style with the list of heroes
653
701
at the top and details of the selected hero below.
654
702
655
703
+ makeExample('toh-4/ts/app/app.component.ts' ,'template' , 'app/heroes.component.ts (current template)' )( format ="." )
656
704
657
705
:marked
706
+ Our goal is to move the detail to its own view and navigate to it when the user decides to edit a selected hero.
707
+
658
708
Delete the `<h1>` at the top (forgot about it during the `AppComponent`-to-`HeroesComponent` conversion).
659
709
660
710
Delete the last line of the template with the `<my-hero-detail>` tags.
@@ -663,8 +713,9 @@ block extract-id
663
713
We're going to display the hero detail on its own page and route to it as we did in the dashboard.
664
714
665
715
But we'll throw in a small twist for variety.
666
- When the user selects a hero from the list, we *won't* go to the detail page.
667
- We'll show a *mini-detail* on *this* page instead and make the user click a button to navigate to the *full detail *page.
716
+ We are keeping the "master/detail" style but shrinking the detail to a "mini", read-only version.
717
+ When the user selects a hero from the list, we *don't* go to the detail page.
718
+ We show a *mini-detail* on *this* page instead and make the user click a button to navigate to the *full detail *page.
668
719
669
720
### Add the *mini-detail*
670
721
@@ -859,6 +910,7 @@ block file-tree-end
859
910
.file app.component.css
860
911
.file app.component.ts
861
912
.file app.module.ts
913
+ .file app-routing.module.ts
862
914
.file dashboard.component.css
863
915
.file dashboard.component.html
864
916
.file dashboard.component.ts
@@ -895,6 +947,7 @@ block file-tree-end
895
947
- We shared the `HeroService` among multiple components.
896
948
- We moved HTML and CSS out of the component file and into their own files.
897
949
- We added the `uppercase` pipe to format data.
950
+ - We refactored routes into a `Routing Module` that we import.
898
951
899
952
### The Road Ahead
900
953
0 commit comments