Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 2e17d58

Browse files
authored
docs(toh-5): add AppRoutingModule (#2584)
1 parent 6f804c6 commit 2e17d58

File tree

6 files changed

+113
-60
lines changed

6 files changed

+113
-60
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// #docregion
2+
import { NgModule } from '@angular/core';
3+
import { RouterModule, Routes } from '@angular/router';
4+
5+
import { DashboardComponent } from './dashboard.component';
6+
import { HeroesComponent } from './heroes.component';
7+
import { HeroDetailComponent } from './hero-detail.component';
8+
9+
const routes: Routes = [
10+
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
11+
{ path: 'dashboard', component: DashboardComponent },
12+
{ path: 'detail/:id', component: HeroDetailComponent },
13+
{ path: 'heroes', component: HeroesComponent }
14+
];
15+
16+
@NgModule({
17+
imports: [ RouterModule.forRoot(routes) ],
18+
exports: [ RouterModule ]
19+
})
20+
export class AppRoutingModule {}
Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,35 @@
1-
// #docplaster
21
// #docregion
32
import { NgModule } from '@angular/core';
43
import { BrowserModule } from '@angular/platform-browser';
54
import { FormsModule } from '@angular/forms';
6-
import { RouterModule } from '@angular/router';
75

86
import { AppComponent } from './app.component';
97
import { DashboardComponent } from './dashboard.component';
108
import { HeroDetailComponent } from './hero-detail.component';
119
import { HeroesComponent } from './heroes.component';
1210
import { HeroService } from './hero.service';
1311

12+
// #docregion routing-module
13+
import { AppRoutingModule } from './app-routing.module';
14+
1415
@NgModule({
1516
imports: [
1617
BrowserModule,
1718
FormsModule,
18-
RouterModule.forRoot([
19-
{
20-
path: '',
21-
redirectTo: '/dashboard',
22-
pathMatch: 'full'
23-
},
24-
{
25-
path: 'dashboard',
26-
component: DashboardComponent
27-
},
28-
{
29-
path: 'detail/:id',
30-
component: HeroDetailComponent
31-
},
32-
{
33-
path: 'heroes',
34-
component: HeroesComponent
35-
}
36-
])
19+
AppRoutingModule
3720
],
38-
// #enddocregion routing
39-
// #docregion dashboard, hero-detail
21+
// #enddocregion routing-module
22+
// #docregion dashboard
4023
declarations: [
4124
AppComponent,
4225
DashboardComponent,
4326
HeroDetailComponent,
4427
HeroesComponent
4528
],
46-
// #enddocregion dashboard, hero-detail
47-
providers: [
48-
HeroService
49-
],
29+
// #enddocregion dashboard
30+
providers: [ HeroService ],
5031
bootstrap: [ AppComponent ]
51-
// #docregion routing
32+
// #docregion routing-module
5233
})
53-
export class AppModule {
54-
}
34+
export class AppModule { }
35+
// #enddocregion routing-module
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { NgModule } from '@angular/core';
2+
import { RouterModule, Routes } from '@angular/router';
3+
4+
import { DashboardComponent } from './dashboard.component';
5+
import { HeroesComponent } from './heroes.component';
6+
import { HeroDetailComponent } from './hero-detail.component';
7+
8+
const routes: Routes = [
9+
{ path: '', redirectTo: '/dashboard', pathMatch: 'full' },
10+
{ path: 'dashboard', component: DashboardComponent },
11+
{ path: 'detail/:id', component: HeroDetailComponent },
12+
{ path: 'heroes', component: HeroesComponent }
13+
];
14+
15+
@NgModule({
16+
imports: [ RouterModule.forRoot(routes) ],
17+
exports: [ RouterModule ]
18+
})
19+
export class AppRoutingModule {}

public/docs/_examples/toh-6/ts/app/app.module.ts

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ import { NgModule } from '@angular/core';
99
import { BrowserModule } from '@angular/platform-browser';
1010
import { FormsModule } from '@angular/forms';
1111
import { HttpModule } from '@angular/http';
12-
import { RouterModule } from '@angular/router';
12+
13+
import { AppRoutingModule } from './app-routing.module';
1314

1415
// #enddocregion v1
1516
// Imports for loading & configuring the in-memory web api
16-
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
17+
import { InMemoryWebApiModule } from 'angular-in-memory-web-api/in-memory-web-api.module';
1718
import { InMemoryDataService } from './in-memory-data.service';
1819

1920
// #docregion v1
@@ -36,25 +37,7 @@ import { HeroSearchComponent } from './hero-search.component';
3637
InMemoryWebApiModule.forRoot(InMemoryDataService),
3738
// #enddocregion in-mem-web-api
3839
// #docregion v1
39-
RouterModule.forRoot([
40-
{
41-
path: '',
42-
redirectTo: '/dashboard',
43-
pathMatch: 'full'
44-
},
45-
{
46-
path: 'dashboard',
47-
component: DashboardComponent
48-
},
49-
{
50-
path: 'detail/:id',
51-
component: HeroDetailComponent
52-
},
53-
{
54-
path: 'heroes',
55-
component: HeroesComponent
56-
}
57-
])
40+
AppRoutingModule
5841
],
5942
// #docregion search
6043
declarations: [
@@ -67,10 +50,7 @@ import { HeroSearchComponent } from './hero-search.component';
6750
// #docregion v1, v2
6851
],
6952
// #enddocregion search
70-
providers: [
71-
HeroService,
72-
],
53+
providers: [ HeroService ],
7354
bootstrap: [ AppComponent ]
7455
})
75-
export class AppModule {
76-
}
56+
export class AppModule { }

public/docs/ts/latest/tutorial/toh-pt5.jade

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,17 +644,67 @@ block extract-id
644644
Refresh the browser and select a hero from the dashboard; the app should navigate directly to that hero’s details.
645645

646646
.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
647694
:marked
648695
## Select a Hero in the *HeroesComponent*
649696

697+
Earlier we added the ability to select a hero from the dashboard.
650698
We'll do something similar in the `HeroesComponent`.
651699

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
653701
at the top and details of the selected hero below.
654702

655703
+makeExample('toh-4/ts/app/app.component.ts','template', 'app/heroes.component.ts (current template)')(format=".")
656704

657705
: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+
658708
Delete the `<h1>` at the top (forgot about it during the `AppComponent`-to-`HeroesComponent` conversion).
659709

660710
Delete the last line of the template with the `<my-hero-detail>` tags.
@@ -663,8 +713,9 @@ block extract-id
663713
We're going to display the hero detail on its own page and route to it as we did in the dashboard.
664714

665715
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.
668719

669720
### Add the *mini-detail*
670721

@@ -859,6 +910,7 @@ block file-tree-end
859910
.file app.component.css
860911
.file app.component.ts
861912
.file app.module.ts
913+
.file app-routing.module.ts
862914
.file dashboard.component.css
863915
.file dashboard.component.html
864916
.file dashboard.component.ts
@@ -895,6 +947,7 @@ block file-tree-end
895947
- We shared the `HeroService` among multiple components.
896948
- We moved HTML and CSS out of the component file and into their own files.
897949
- We added the `uppercase` pipe to format data.
950+
- We refactored routes into a `Routing Module` that we import.
898951

899952
### The Road Ahead
900953

public/docs/ts/latest/tutorial/toh-pt6.jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ block filetree
584584
- We configured an in-memory web API.
585585
- We learned how to use !{_Observable}s.
586586

587-
Here are the files we added or changed in this chapter.
587+
Here are the files we _added or changed_ in this chapter.
588588

589589
block file-summary
590590
+makeTabs(

0 commit comments

Comments
 (0)