docs(router): Updated usage of observables in router tutorial and developer guide (#2696)

Moved route configuration into separate variable for consistency
Added async pipe to handle subscriptions for list items
This commit is contained in:
Ward Bell 2016-11-14 12:43:09 -08:00 committed by GitHub
commit 5c4cc9a3c8
42 changed files with 540 additions and 571 deletions

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { AdminComponent } from './admin.component'; import { AdminComponent } from './admin.component';
import { AdminDashboardComponent } from './admin-dashboard.component'; import { AdminDashboardComponent } from './admin-dashboard.component';
@ -9,9 +9,7 @@ import { ManageCrisesComponent } from './manage-crises.component';
import { ManageHeroesComponent } from './manage-heroes.component'; import { ManageHeroesComponent } from './manage-heroes.component';
// #docregion admin-routes // #docregion admin-routes
@NgModule({ const adminRoutes: Routes = [
imports: [
RouterModule.forChild([
{ {
path: 'admin', path: 'admin',
component: AdminComponent, component: AdminComponent,
@ -26,7 +24,11 @@ import { ManageHeroesComponent } from './manage-heroes.component';
} }
] ]
} }
]) ];
@NgModule({
imports: [
RouterModule.forChild(adminRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { AdminComponent } from './admin.component'; import { AdminComponent } from './admin.component';
import { AdminDashboardComponent } from './admin-dashboard.component'; import { AdminDashboardComponent } from './admin-dashboard.component';
@ -11,9 +11,7 @@ import { ManageHeroesComponent } from './manage-heroes.component';
// #docregion admin-route, can-activate-child // #docregion admin-route, can-activate-child
import { AuthGuard } from '../auth-guard.service'; import { AuthGuard } from '../auth-guard.service';
@NgModule({ const adminRoutes: Routes = [
imports: [
RouterModule.forChild([
{ {
path: 'admin', path: 'admin',
component: AdminComponent, component: AdminComponent,
@ -33,7 +31,11 @@ import { AuthGuard } from '../auth-guard.service';
} }
] ]
} }
]) ];
@NgModule({
imports: [
RouterModule.forChild(adminRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { AdminComponent } from './admin.component'; import { AdminComponent } from './admin.component';
import { AdminDashboardComponent } from './admin-dashboard.component'; import { AdminDashboardComponent } from './admin-dashboard.component';
@ -12,9 +12,7 @@ import { ManageHeroesComponent } from './manage-heroes.component';
import { AuthGuard } from '../auth-guard.service'; import { AuthGuard } from '../auth-guard.service';
// #docregion can-activate-child // #docregion can-activate-child
@NgModule({ const adminRoutes: Routes = [
imports: [
RouterModule.forChild([
{ {
path: 'admin', path: 'admin',
component: AdminComponent, component: AdminComponent,
@ -31,7 +29,11 @@ import { AuthGuard } from '../auth-guard.service';
} }
] ]
} }
]) ];
@NgModule({
imports: [
RouterModule.forChild(adminRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { AdminComponent } from './admin.component'; import { AdminComponent } from './admin.component';
import { AdminDashboardComponent } from './admin-dashboard.component'; import { AdminDashboardComponent } from './admin-dashboard.component';
@ -11,9 +11,7 @@ import { ManageHeroesComponent } from './manage-heroes.component';
// #docregion admin-route // #docregion admin-route
import { AuthGuard } from '../auth-guard.service'; import { AuthGuard } from '../auth-guard.service';
@NgModule({ const adminRoutes: Routes = [
imports: [
RouterModule.forChild([
{ {
path: '', path: '',
component: AdminComponent, component: AdminComponent,
@ -30,7 +28,11 @@ import { AuthGuard } from '../auth-guard.service';
} }
] ]
} }
]) ];
@NgModule({
imports: [
RouterModule.forChild(adminRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,17 +1,19 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { CrisisListComponent } from './crisis-list.component'; import { CrisisListComponent } from './crisis-list.component';
import { HeroListComponent } from './hero-list.component'; import { HeroListComponent } from './hero-list.component';
@NgModule({ const appRoutes: Routes = [
imports: [
RouterModule.forRoot([
{ path: 'crisis-center', component: CrisisListComponent }, { path: 'crisis-center', component: CrisisListComponent },
{ path: 'heroes', component: HeroListComponent } { path: 'heroes', component: HeroListComponent }
]) ];
@NgModule({
imports: [
RouterModule.forRoot(appRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,15 +1,17 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { CrisisListComponent } from './crisis-list.component'; import { CrisisListComponent } from './crisis-list.component';
const appRoutes: Routes = [
{ path: 'crisis-center', component: CrisisListComponent }
];
@NgModule({ @NgModule({
imports: [ imports: [
RouterModule.forRoot([ RouterModule.forRoot(appRoutes)
{ path: 'crisis-center', component: CrisisListComponent },
])
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,13 +1,15 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
const appRoutes: Routes = [
];
@NgModule({ @NgModule({
imports: [ imports: [
RouterModule.forRoot([ RouterModule.forRoot(appRoutes)
])
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,15 +1,17 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { CanDeactivateGuard } from './can-deactivate-guard.service'; import { CanDeactivateGuard } from './can-deactivate-guard.service';
const appRoutes: Routes = [
];
@NgModule({ @NgModule({
imports: [ imports: [
RouterModule.forRoot([ RouterModule.forRoot(appRoutes)
])
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -2,7 +2,7 @@
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
// #docregion import-router // #docregion import-router
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
// #enddocregion import-router // #enddocregion import-router
import { CanDeactivateGuard } from './can-deactivate-guard.service'; import { CanDeactivateGuard } from './can-deactivate-guard.service';
@ -11,17 +11,20 @@ import { AuthGuard } from './auth-guard.service';
// #enddocregion can-load-guard // #enddocregion can-load-guard
// #docregion lazy-load-admin, can-load-guard // #docregion lazy-load-admin, can-load-guard
@NgModule({
imports: [ const appRoutes: Routes = [
RouterModule.forRoot([
{ {
path: 'admin', path: 'admin',
loadChildren: 'app/admin/admin.module#AdminModule', loadChildren: 'app/admin/admin.module#AdminModule',
// #enddocregion lazy-load-admin // #enddocregion lazy-load-admin
canLoad: [AuthGuard] canLoad: [AuthGuard]
// #docregion lazy-load-admin // #docregion lazy-load-admin
} }
]) ];
@NgModule({
imports: [
RouterModule.forRoot(appRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -2,7 +2,7 @@
// #docregion, preload-v1 // #docregion, preload-v1
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { import {
RouterModule, RouterModule, Routes,
// #enddocregion preload-v1 // #enddocregion preload-v1
PreloadAllModules PreloadAllModules
// #docregion preload-v1 // #docregion preload-v1
@ -11,9 +11,7 @@ import {
import { CanDeactivateGuard } from './can-deactivate-guard.service'; import { CanDeactivateGuard } from './can-deactivate-guard.service';
import { AuthGuard } from './auth-guard.service'; import { AuthGuard } from './auth-guard.service';
@NgModule({ const appRoutes: Routes = [
imports: [
RouterModule.forRoot([
{ {
path: 'admin', path: 'admin',
loadChildren: 'app/admin/admin.module#AdminModule', loadChildren: 'app/admin/admin.module#AdminModule',
@ -27,10 +25,15 @@ import { AuthGuard } from './auth-guard.service';
{ {
path: 'crisis-center', path: 'crisis-center',
loadChildren: 'app/crisis-center/crisis-center.module#CrisisCenterModule' loadChildren: 'app/crisis-center/crisis-center.module#CrisisCenterModule'
}, }
], ];
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes
// #enddocregion preload-v1 // #enddocregion preload-v1
{ preloadingStrategy: PreloadAllModules } , { preloadingStrategy: PreloadAllModules }
// #docregion preload-v1 // #docregion preload-v1
) )
], ],

View File

@ -1,15 +1,13 @@
// #docplaster // #docplaster
// #docregion, preload-v1 // #docregion, preload-v1
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { CanDeactivateGuard } from './can-deactivate-guard.service'; import { CanDeactivateGuard } from './can-deactivate-guard.service';
import { AuthGuard } from './auth-guard.service'; import { AuthGuard } from './auth-guard.service';
import { PreloadSelectedModules } from './selective-preload-strategy'; import { PreloadSelectedModules } from './selective-preload-strategy';
@NgModule({ const appRoutes: Routes = [
imports: [
RouterModule.forRoot([
{ {
path: 'admin', path: 'admin',
loadChildren: 'app/admin/admin.module#AdminModule', loadChildren: 'app/admin/admin.module#AdminModule',
@ -29,8 +27,14 @@ import { PreloadSelectedModules } from './selective-preload-strategy';
} }
} }
// #enddocregion preload-v2 // #enddocregion preload-v2
], ];
{ preloadingStrategy: PreloadSelectedModules })
@NgModule({
imports: [
RouterModule.forRoot(
appRoutes,
{ preloadingStrategy: PreloadSelectedModules }
)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -4,7 +4,7 @@
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'; import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
import { HeroListComponent } from './hero-list.component'; import { HeroListComponent } from './hero-list.component';
@ -14,11 +14,7 @@ import { PageNotFoundComponent as HeroDetailComponent } from './not-found.compon
import { PageNotFoundComponent as HomeComponent } from './not-found.component'; import { PageNotFoundComponent as HomeComponent } from './not-found.component';
// #docregion route-config // #docregion route-config
@NgModule({ const appRoutes: Routes = [
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot([
// #docregion route-defs // #docregion route-defs
// #docregion hero-detail-route // #docregion hero-detail-route
{ path: 'hero/:id', component: HeroDetailComponent }, { path: 'hero/:id', component: HeroDetailComponent },
@ -34,7 +30,13 @@ import { PageNotFoundComponent as HomeComponent } from './not-found.component';
{ path: '', component: HomeComponent }, { path: '', component: HomeComponent },
// #enddocregion route-defs // #enddocregion route-defs
{ path: '**', component: PageNotFoundComponent } { path: '**', component: PageNotFoundComponent }
]) ];
@NgModule({
imports: [
BrowserModule,
FormsModule,
RouterModule.forRoot(appRoutes)
], ],
declarations: [ declarations: [
AppComponent, AppComponent,

View File

@ -4,7 +4,7 @@ import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser'; import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
// #docregion import-router, route-config // #docregion import-router, route-config
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
// #enddocregion import-router, route-config // #enddocregion import-router, route-config
// #docregion router-basics // #docregion router-basics
@ -12,16 +12,18 @@ import { AppComponent } from './app.component';
import { CrisisListComponent } from './crisis-list.component'; import { CrisisListComponent } from './crisis-list.component';
import { HeroListComponent } from './hero-list.component'; import { HeroListComponent } from './hero-list.component';
// #docregion route-config
const appRoutes: Routes = [
{ path: 'crisis-center', component: CrisisListComponent },
{ path: 'heroes', component: HeroListComponent }
];
// #enddocregion route-config
@NgModule({ @NgModule({
imports: [ imports: [
BrowserModule, BrowserModule,
FormsModule, FormsModule,
// #docregion route-config RouterModule.forRoot(appRoutes)
RouterModule.forRoot([
{ path: 'crisis-center', component: CrisisListComponent },
{ path: 'heroes', component: HeroListComponent }
])
// #enddocregion route-config
], ],
declarations: [ declarations: [
AppComponent, AppComponent,

View File

@ -1,44 +0,0 @@
// #docplaster
// #docregion
// #docregion route-config
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
// #enddocregion route-config
// #enddocregion
// #docregion base-routes
import { HeroListComponent } from './hero-list.component';
import { CrisisCenterComponent } from './crisis-center/crisis-center.component';
import { HeroDetailComponent } from './heroes/hero-detail.component';
import { PageNotFoundComponent } from './not-found.component';
import { PageNotFoundComponent as HomeComponent } from './not-found.component';
// #enddocregion base-routes
// #docregion
// #docregion route-config
const appRoutes: Routes = [
// #docregion route-defs
// #docregion hero-detail-route
{ path: 'hero/:id', component: HeroDetailComponent },
// #enddocregion hero-detail-route
{ path: 'crisis-center', component: CrisisCenterComponent },
{
path: 'heroes',
component: HeroListComponent,
data: {
title: 'Heroes List'
}
},
{ path: '', component: HomeComponent },
// #enddocregion route-defs
{ path: '**', component: PageNotFoundComponent }
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
// #enddocregion route-config
// #enddocregion

View File

@ -1,22 +0,0 @@
// #docplaster
// #docregion
import { ModuleWithProviders } from '@angular/core';
// #docregion route-config
import { Routes, RouterModule } from '@angular/router';
// #enddocregion route-config
import { CrisisListComponent } from './crisis-list.component';
import { HeroListComponent } from './hero-list.component';
// #docregion route-config
const appRoutes: Routes = [
{ path: 'crisis-center', component: CrisisListComponent },
{ path: 'heroes', component: HeroListComponent }
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);
// #enddocregion route-config

View File

@ -1,16 +0,0 @@
// #docplaster
// #docregion
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CrisisCenterComponent } from './crisis-center/crisis-center.component';
const appRoutes: Routes = [
{ path: 'crisis-center', component: CrisisCenterComponent }
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

View File

@ -1,13 +0,0 @@
// #docregion
import { ModuleWithProviders } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
const appRoutes: Routes = [
];
export const appRoutingProviders: any[] = [
];
export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

View File

@ -1,16 +0,0 @@
// // #docregion
// import { ModuleWithProviders } from '@angular/core';
// import { Routes, RouterModule } from '@angular/router';
//
// import { loginRoutes,
// authProviders } from './login.routing';
//
// const appRoutes: Routes = [
// ...loginRoutes
// ];
//
// export const appRoutingProviders: any[] = [
// authProviders
// ];
//
// export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

View File

@ -1,21 +0,0 @@
// // #docregion
// import { ModuleWithProviders } from '@angular/core';
// // #docregion import-router
// import { Routes, RouterModule } from '@angular/router';
// // #enddocregion import-router
//
// import { loginRoutes,
// authProviders } from './login.routing';
//
// import { CanDeactivateGuard } from './can-deactivate-guard.service';
//
// const appRoutes: Routes = [
// ...loginRoutes
// ];
//
// export const appRoutingProviders: any[] = [
// authProviders,
// CanDeactivateGuard
// ];
//
// export const routing: ModuleWithProviders = RouterModule.forRoot(appRoutes);

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { CrisisCenterHomeComponent } from './crisis-center-home.component'; import { CrisisCenterHomeComponent } from './crisis-center-home.component';
import { CrisisListComponent } from './crisis-list.component'; import { CrisisListComponent } from './crisis-list.component';
@ -9,9 +9,7 @@ import { CrisisCenterComponent } from './crisis-center.component';
import { CrisisDetailComponent } from './crisis-detail.component'; import { CrisisDetailComponent } from './crisis-detail.component';
// #docregion routes // #docregion routes
@NgModule({ const crisisCenterRoutes: Routes = [
imports: [
RouterModule.forChild([
{ {
path: 'crisis-center', path: 'crisis-center',
component: CrisisCenterComponent, component: CrisisCenterComponent,
@ -32,7 +30,11 @@ import { CrisisDetailComponent } from './crisis-detail.component';
} }
] ]
} }
]) ];
@NgModule({
imports: [
RouterModule.forChild(crisisCenterRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion routes // #docregion routes
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { CrisisCenterHomeComponent } from './crisis-center-home.component'; import { CrisisCenterHomeComponent } from './crisis-center-home.component';
import { CrisisListComponent } from './crisis-list.component'; import { CrisisListComponent } from './crisis-list.component';
@ -18,10 +18,8 @@ import { CrisisDetailResolve } from './crisis-detail-resolve.service';
// #enddocregion crisis-detail-resolve // #enddocregion crisis-detail-resolve
// #docregion routes // #docregion routes
@NgModule({ const crisisCenterRoutes: Routes = [
imports: [ // #enddocregion routes
RouterModule.forChild([
// #enddocregion routes
// #docregion redirect, routes // #docregion redirect, routes
{ {
path: '', path: '',
@ -61,7 +59,11 @@ import { CrisisDetailResolve } from './crisis-detail-resolve.service';
] ]
} }
// #docregion routes // #docregion routes
]) ];
@NgModule({
imports: [
RouterModule.forChild(crisisCenterRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { CrisisCenterHomeComponent } from './crisis-center-home.component'; import { CrisisCenterHomeComponent } from './crisis-center-home.component';
import { CrisisListComponent } from './crisis-list.component'; import { CrisisListComponent } from './crisis-list.component';
@ -11,9 +11,7 @@ import { CrisisDetailComponent } from './crisis-detail.component';
// #docregion can-deactivate-guard // #docregion can-deactivate-guard
import { CanDeactivateGuard } from '../can-deactivate-guard.service'; import { CanDeactivateGuard } from '../can-deactivate-guard.service';
@NgModule({ const crisisCenterRoutes: Routes = [
imports: [
RouterModule.forChild([
{ {
path: '', path: '',
redirectTo: '/crisis-center', redirectTo: '/crisis-center',
@ -40,7 +38,11 @@ import { CanDeactivateGuard } from '../can-deactivate-guard.service';
} }
] ]
} }
]) ];
@NgModule({
imports: [
RouterModule.forChild(crisisCenterRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { CrisisCenterHomeComponent } from './crisis-center-home.component'; import { CrisisCenterHomeComponent } from './crisis-center-home.component';
import { CrisisListComponent } from './crisis-list.component'; import { CrisisListComponent } from './crisis-list.component';
@ -13,9 +13,7 @@ import { CanDeactivateGuard } from '../can-deactivate-guard.service';
// #docregion crisis-detail-resolve // #docregion crisis-detail-resolve
import { CrisisDetailResolve } from './crisis-detail-resolve.service'; import { CrisisDetailResolve } from './crisis-detail-resolve.service';
@NgModule({ const crisisCenterRoutes: Routes = [
imports: [
RouterModule.forChild([
// #docregion redirect // #docregion redirect
{ {
path: '', path: '',
@ -47,7 +45,11 @@ import { CrisisDetailResolve } from './crisis-detail-resolve.service';
} }
] ]
} }
]) ];
@NgModule({
imports: [
RouterModule.forChild(crisisCenterRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { CrisisCenterHomeComponent } from './crisis-center-home.component'; import { CrisisCenterHomeComponent } from './crisis-center-home.component';
import { CrisisListComponent } from './crisis-list.component'; import { CrisisListComponent } from './crisis-list.component';
@ -13,9 +13,7 @@ import { CanDeactivateGuard } from '../can-deactivate-guard.service';
// #docregion crisis-detail-resolve // #docregion crisis-detail-resolve
import { CrisisDetailResolve } from './crisis-detail-resolve.service'; import { CrisisDetailResolve } from './crisis-detail-resolve.service';
@NgModule({ const crisisCenterRoutes: Routes = [
imports: [
RouterModule.forChild([
{ {
path: '', path: '',
component: CrisisCenterComponent, component: CrisisCenterComponent,
@ -40,7 +38,11 @@ import { CrisisDetailResolve } from './crisis-detail-resolve.service';
} }
] ]
} }
]) ];
@NgModule({
imports: [
RouterModule.forChild(crisisCenterRoutes)
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -10,7 +10,7 @@ export class CrisisDetailResolve implements Resolve<Crisis> {
constructor(private cs: CrisisService, private router: Router) {} constructor(private cs: CrisisService, private router: Router) {}
resolve(route: ActivatedRouteSnapshot): Promise<Crisis>|boolean { resolve(route: ActivatedRouteSnapshot): Promise<Crisis>|boolean {
let id = +route.params['id']; let id = route.params['id'];
return this.cs.getCrisis(id).then(crisis => { return this.cs.getCrisis(id).then(crisis => {
if (crisis) { if (crisis) {

View File

@ -1,5 +1,6 @@
// #docplaster // #docplaster
// #docregion // #docregion
import 'rxjs/add/operator/switchMap';
import { Component, OnInit, import { Component, OnInit,
HostBinding, trigger, transition, HostBinding, trigger, transition,
animate, style, state } from '@angular/core'; animate, style, state } from '@angular/core';
@ -12,9 +13,9 @@ import { DialogService } from '../dialog.service';
// #docregion template // #docregion template
template: ` template: `
<div *ngIf="crisis"> <div *ngIf="crisis">
<h3>"{{editName}}"</h3> <h3>"{{ editName }}"</h3>
<div> <div>
<label>Id: </label>{{crisis.id}}</div> <label>Id: </label>{{ crisis.id }}</div>
<div> <div>
<label>Name: </label> <label>Name: </label>
<input [(ngModel)]="editName" placeholder="name"/> <input [(ngModel)]="editName" placeholder="name"/>
@ -74,14 +75,13 @@ export class CrisisDetailComponent implements OnInit {
private router: Router, private router: Router,
private route: ActivatedRoute, private route: ActivatedRoute,
public dialogService: DialogService public dialogService: DialogService
) { } ) {}
// #docregion ngOnInit // #docregion ngOnInit
ngOnInit() { ngOnInit() {
this.route.params.forEach((params: Params) => { this.route.params
let id = +params['id']; .switchMap((params: Params) => this.service.getCrisis(params['id']))
this.service.getCrisis(id) .subscribe((crisis: Crisis) => {
.then(crisis => {
if (crisis) { if (crisis) {
this.editName = crisis.name; this.editName = crisis.name;
this.crisis = crisis; this.crisis = crisis;
@ -89,7 +89,6 @@ export class CrisisDetailComponent implements OnInit {
this.gotoCrises(); this.gotoCrises();
} }
}); });
});
} }
// #enddocregion ngOnInit // #enddocregion ngOnInit

View File

@ -11,9 +11,9 @@ import { DialogService } from '../dialog.service';
@Component({ @Component({
template: ` template: `
<div *ngIf="crisis"> <div *ngIf="crisis">
<h3>"{{editName}}"</h3> <h3>"{{ editName }}"</h3>
<div> <div>
<label>Id: </label>{{crisis.id}}</div> <label>Id: </label>{{ crisis.id }}</div>
<div> <div>
<label>Name: </label> <label>Name: </label>
<input [(ngModel)]="editName" placeholder="name"/> <input [(ngModel)]="editName" placeholder="name"/>
@ -70,11 +70,12 @@ export class CrisisDetailComponent implements OnInit {
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
public dialogService: DialogService public dialogService: DialogService
) { } ) {}
// #docregion crisis-detail-resolve // #docregion crisis-detail-resolve
ngOnInit() { ngOnInit() {
this.route.data.forEach((data: { crisis: Crisis }) => { this.route.data
.subscribe((data: { crisis: Crisis }) => {
this.editName = data.crisis.name; this.editName = data.crisis.name;
this.crisis = data.crisis; this.crisis = data.crisis;
}); });

View File

@ -1,49 +1,45 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { Component, OnInit, OnDestroy } from '@angular/core'; import 'rxjs/add/operator/do';
import { ActivatedRoute, Router } from '@angular/router'; import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, Params } from '@angular/router';
import { Crisis, CrisisService } from './crisis.service'; import { Crisis, CrisisService } from './crisis.service';
import { Subscription } from 'rxjs/Subscription'; import { Observable } from 'rxjs/Observable';
@Component({ @Component({
// #docregion template // #docregion template
template: ` template: `
<ul class="items"> <ul class="items">
<li *ngFor="let crisis of crises" <li *ngFor="let crisis of crises | async"
(click)="onSelect(crisis)"> (click)="onSelect(crisis)">
<span class="badge">{{crisis.id}}</span> {{crisis.name}} <span class="badge">{{ crisis.id }}</span> {{ crisis.name }}
</li> </li>
</ul> </ul>
`, `,
// #enddocregion template // #enddocregion template
}) })
export class CrisisListComponent implements OnInit, OnDestroy { export class CrisisListComponent implements OnInit {
crises: Crisis[]; crises: Observable<Crisis[]>;
selectedId: number; selectedId: number;
private sub: Subscription;
// #docregion relative-navigation-ctor // #docregion relative-navigation-ctor
constructor( constructor(
private service: CrisisService, private service: CrisisService,
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router) {} private router: Router
) {}
// #enddocregion relative-navigation-ctor // #enddocregion relative-navigation-ctor
ngOnInit() { ngOnInit() {
this.sub = this.route this.crises = this.route.params
.params .switchMap((params: Params) => {
.subscribe(params => {
this.selectedId = +params['id']; this.selectedId = +params['id'];
this.service.getCrises() return this.service.getCrises();
.then(crises => this.crises = crises);
}); });
} }
ngOnDestroy() {
this.sub.unsubscribe();
}
// #docregion select // #docregion select
onSelect(crisis: Crisis) { onSelect(crisis: Crisis) {
// Absolute link // Absolute link

View File

@ -1,17 +1,20 @@
// #docplaster // #docplaster
// #docregion // #docregion
import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, Params } from '@angular/router'; import { ActivatedRoute, Router, Params } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import { Crisis, CrisisService } from './crisis.service'; import { Crisis, CrisisService } from './crisis.service';
@Component({ @Component({
template: ` template: `
<ul class="items"> <ul class="items">
<li *ngFor="let crisis of crises" <li *ngFor="let crisis of crises | async"
[class.selected]="isSelected(crisis)" [class.selected]="isSelected(crisis)"
(click)="onSelect(crisis)"> (click)="onSelect(crisis)">
<span class="badge">{{crisis.id}}</span> {{crisis.name}} <span class="badge">{{ crisis.id }}</span> {{ crisis.name }}
</li> </li>
</ul> </ul>
@ -19,24 +22,24 @@ import { Crisis, CrisisService } from './crisis.service';
` `
}) })
export class CrisisListComponent implements OnInit { export class CrisisListComponent implements OnInit {
crises: Crisis[]; crises: Observable<Crisis[]>;
public selectedId: number; selectedId: number;
constructor( constructor(
private service: CrisisService, private service: CrisisService,
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router private router: Router
) { } ) {}
isSelected(crisis: Crisis) { isSelected(crisis: Crisis) {
return crisis.id === this.selectedId; return crisis.id === this.selectedId;
} }
ngOnInit() { ngOnInit() {
this.route.params.forEach((params: Params) => { this.crises = this.route.params
this.selectedId = params['id']; .switchMap((params: Params) => {
this.service.getCrises() this.selectedId = +params['id'];
.then(crises => this.crises = crises); return this.service.getCrises();
}); });
} }

View File

@ -1,5 +1,8 @@
// #docplaster // #docplaster
// #docregion // #docregion
// #docregion rxjs-operator-import
import 'rxjs/add/operator/switchMap';
// #enddocregion rxjs-operator-import
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
// #docregion imports // #docregion imports
import { Router, ActivatedRoute, Params } from '@angular/router'; import { Router, ActivatedRoute, Params } from '@angular/router';
@ -11,9 +14,9 @@ import { Hero, HeroService } from './hero.service';
template: ` template: `
<h2>HEROES</h2> <h2>HEROES</h2>
<div *ngIf="hero"> <div *ngIf="hero">
<h3>"{{hero.name}}"</h3> <h3>"{{ hero.name }}"</h3>
<div> <div>
<label>Id: </label>{{hero.id}}</div> <label>Id: </label>{{ hero.id }}</div>
<div> <div>
<label>Name: </label> <label>Name: </label>
<input [(ngModel)]="hero.name" placeholder="name"/> <input [(ngModel)]="hero.name" placeholder="name"/>
@ -37,14 +40,16 @@ export class HeroDetailComponent implements OnInit {
// #docregion ngOnInit // #docregion ngOnInit
ngOnInit() { ngOnInit() {
this.route.params.forEach((params: Params) => { this.route.params
let id = +params['id']; // (+) converts string 'id' to a number // (+) converts string 'id' to a number
this.service.getHero(id).then(hero => this.hero = hero); .switchMap((params: Params) => this.service.getHero(+params['id']))
}); .subscribe((hero: Hero) => this.hero = hero);
} }
// #enddocregion ngOnInit // #enddocregion ngOnInit
// #docregion gotoHeroes // #docregion gotoHeroes
gotoHeroes() { this.router.navigate(['/heroes']); } gotoHeroes() {
this.router.navigate(['/heroes']);
}
// #enddocregion gotoHeroes // #enddocregion gotoHeroes
} }

View File

@ -9,9 +9,9 @@ import { Hero, HeroService } from './hero.service';
template: ` template: `
<h2>HEROES</h2> <h2>HEROES</h2>
<div *ngIf="hero"> <div *ngIf="hero">
<h3>"{{hero.name}}"</h3> <h3>"{{ hero.name }}"</h3>
<div> <div>
<label>Id: </label>{{hero.id}}</div> <label>Id: </label>{{ hero.id }}</div>
<div> <div>
<label>Name: </label> <label>Name: </label>
<input [(ngModel)]="hero.name" placeholder="name"/> <input [(ngModel)]="hero.name" placeholder="name"/>
@ -28,15 +28,20 @@ export class HeroDetailComponent implements OnInit {
constructor( constructor(
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private service: HeroService) {} private service: HeroService
) {}
// #docregion snapshot // #docregion snapshot
ngOnInit() { ngOnInit() {
// (+) converts string 'id' to a number // (+) converts string 'id' to a number
let id = +this.route.snapshot.params['id']; let id = +this.route.snapshot.params['id'];
this.service.getHero(id).then(hero => this.hero = hero);
this.service.getHero(id)
.then((hero: Hero) => this.hero = hero);
} }
// #enddocregion snapshot // #enddocregion snapshot
gotoHeroes() { this.router.navigate(['/heroes']); } gotoHeroes() {
this.router.navigate(['/heroes']);
}
} }

View File

@ -1,5 +1,8 @@
// #docplaster // #docplaster
// #docregion // #docregion
// #docregion rxjs-operator-import
import 'rxjs/add/operator/switchMap';
// #docregion rxjs-operator-import
// #docregion route-animation-imports // #docregion route-animation-imports
import { Component, OnInit, HostBinding, import { Component, OnInit, HostBinding,
trigger, transition, animate, trigger, transition, animate,
@ -14,9 +17,9 @@ import { Hero, HeroService } from './hero.service';
template: ` template: `
<h2>HEROES</h2> <h2>HEROES</h2>
<div *ngIf="hero"> <div *ngIf="hero">
<h3>"{{hero.name}}"</h3> <h3>"{{ hero.name }}"</h3>
<div> <div>
<label>Id: </label>{{hero.id}}</div> <label>Id: </label>{{ hero.id }}</div>
<div> <div>
<label>Name: </label> <label>Name: </label>
<input [(ngModel)]="hero.name" placeholder="name"/> <input [(ngModel)]="hero.name" placeholder="name"/>
@ -71,15 +74,16 @@ export class HeroDetailComponent implements OnInit {
constructor( constructor(
private route: ActivatedRoute, private route: ActivatedRoute,
private router: Router, private router: Router,
private service: HeroService) {} private service: HeroService
) {}
// #enddocregion ctor // #enddocregion ctor
// #docregion ngOnInit // #docregion ngOnInit
ngOnInit() { ngOnInit() {
this.route.params.forEach((params: Params) => { this.route.params
let id = +params['id']; // (+) converts string 'id' to a number // (+) converts string 'id' to a number
this.service.getHero(id).then(hero => this.hero = hero); .switchMap((params: Params) => this.service.getHero(+params['id']))
}); .subscribe((hero: Hero) => this.hero = hero);
} }
// #enddocregion ngOnInit // #enddocregion ngOnInit

View File

@ -11,25 +11,26 @@ import { Hero, HeroService } from './hero.service';
template: ` template: `
<h2>HEROES</h2> <h2>HEROES</h2>
<ul class="items"> <ul class="items">
<li *ngFor="let hero of heroes" <li *ngFor="let hero of heroes | async"
(click)="onSelect(hero)"> (click)="onSelect(hero)">
<span class="badge">{{hero.id}}</span> {{hero.name}} <span class="badge">{{ hero.id }}</span> {{ hero.name }}
</li> </li>
</ul> </ul>
` `
// #enddocregion template // #enddocregion template
}) })
export class HeroListComponent implements OnInit { export class HeroListComponent implements OnInit {
heroes: Hero[]; heroes: Promise<Hero[]>;
// #docregion ctor // #docregion ctor
constructor( constructor(
private router: Router, private router: Router,
private service: HeroService) { } private service: HeroService
) {}
// #enddocregion ctor // #enddocregion ctor
ngOnInit() { ngOnInit() {
this.service.getHeroes().then(heroes => this.heroes = heroes); this.heroes = this.service.getHeroes();
} }
// #docregion select // #docregion select

View File

@ -1,6 +1,10 @@
// #docplaster // #docplaster
// #docregion // #docregion
// TODO SOMEDAY: Feature Componetized like CrisisCenter // TODO SOMEDAY: Feature Componetized like CrisisCenter
// #docregion rxjs-imports
import 'rxjs/add/operator/switchMap';
import { Observable } from 'rxjs/Observable';
// #enddocregion rxjs-imports
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
// #docregion import-router // #docregion import-router
import { Router, ActivatedRoute, Params } from '@angular/router'; import { Router, ActivatedRoute, Params } from '@angular/router';
@ -13,19 +17,19 @@ import { Hero, HeroService } from './hero.service';
template: ` template: `
<h2>HEROES</h2> <h2>HEROES</h2>
<ul class="items"> <ul class="items">
<li *ngFor="let hero of heroes" <li *ngFor="let hero of heroes | async"
[class.selected]="isSelected(hero)" [class.selected]="isSelected(hero)"
(click)="onSelect(hero)"> (click)="onSelect(hero)">
<span class="badge">{{hero.id}}</span> {{hero.name}} <span class="badge">{{ hero.id }}</span> {{ hero.name }}
</li> </li>
</ul> </ul>
` `
// #enddocregion template // #enddocregion template
}) })
// #docregion ctor
export class HeroListComponent implements OnInit { export class HeroListComponent implements OnInit {
heroes: Hero[]; heroes: Observable<Hero[]>;
// #docregion ctor
private selectedId: number; private selectedId: number;
constructor( constructor(
@ -36,10 +40,10 @@ export class HeroListComponent implements OnInit {
// #enddocregion ctor // #enddocregion ctor
ngOnInit() { ngOnInit() {
this.route.params.forEach((params: Params) => { this.heroes = this.route.params
.switchMap((params: Params) => {
this.selectedId = +params['id']; this.selectedId = +params['id'];
this.service.getHeroes() return this.service.getHeroes();
.then(heroes => this.heroes = heroes);
}); });
} }
// #enddocregion ctor // #enddocregion ctor
@ -53,6 +57,6 @@ export class HeroListComponent implements OnInit {
this.router.navigate(['/hero', hero.id]); this.router.navigate(['/hero', hero.id]);
} }
// #enddocregion select // #enddocregion select
// #docregion ctor
} }
// #enddocregion // #enddocregion

View File

@ -1,18 +1,20 @@
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { HeroListComponent } from './hero-list.component'; import { HeroListComponent } from './hero-list.component';
import { HeroDetailComponent } from './hero-detail.component'; import { HeroDetailComponent } from './hero-detail.component';
const heroesRoutes: Routes = [
{ path: 'heroes', component: HeroListComponent },
// #docregion hero-detail-route
{ path: 'hero/:id', component: HeroDetailComponent }
// #enddocregion hero-detail-route
];
@NgModule({ @NgModule({
imports: [ imports: [
RouterModule.forChild([ RouterModule.forChild(heroesRoutes)
{ path: 'heroes', component: HeroListComponent },
// #docregion hero-detail-route
{ path: 'hero/:id', component: HeroDetailComponent }
// #enddocregion hero-detail-route
])
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -1,15 +1,17 @@
// #docregion // #docregion
import { NgModule } from '@angular/core'; import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router'; import { RouterModule, Routes } from '@angular/router';
import { AuthGuard } from './auth-guard.service'; import { AuthGuard } from './auth-guard.service';
import { AuthService } from './auth.service'; import { AuthService } from './auth.service';
import { LoginComponent } from './login.component'; import { LoginComponent } from './login.component';
const loginRoutes: Routes = [
{ path: 'login', component: LoginComponent }
];
@NgModule({ @NgModule({
imports: [ imports: [
RouterModule.forChild([ RouterModule.forChild(loginRoutes)
{ path: 'login', component: LoginComponent }
])
], ],
exports: [ exports: [
RouterModule RouterModule

View File

@ -6,4 +6,3 @@ import { Component } from '@angular/core';
template: '<h3>My Dashboard</h3>' template: '<h3>My Dashboard</h3>'
}) })
export class DashboardComponent { } export class DashboardComponent { }

View File

@ -1,5 +1,7 @@
// #docplaster // #docplaster
// #docregion , v2 // #docregion , v2, rxjs-import
import 'rxjs/add/operator/switchMap';
// #enddocregion rxjs-import
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router'; import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common'; import { Location } from '@angular/common';
@ -31,11 +33,9 @@ export class HeroDetailComponent implements OnInit {
// #docregion ngOnInit // #docregion ngOnInit
ngOnInit(): void { ngOnInit(): void {
this.route.params.forEach((params: Params) => { this.route.params
let id = +params['id']; .switchMap((params: Params) => this.heroService.getHero(+params['id']))
this.heroService.getHero(id) .subscribe(hero => this.hero = hero);
.then(hero => this.hero = hero);
});
} }
// #enddocregion ngOnInit // #enddocregion ngOnInit

View File

@ -1,4 +1,5 @@
// #docregion // #docregion
import 'rxjs/add/operator/switchMap';
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router'; import { ActivatedRoute, Params } from '@angular/router';
import { Location } from '@angular/common'; import { Location } from '@angular/common';
@ -22,11 +23,9 @@ export class HeroDetailComponent implements OnInit {
) {} ) {}
ngOnInit(): void { ngOnInit(): void {
this.route.params.forEach((params: Params) => { this.route.params
let id = +params['id']; .switchMap((params: Params) => this.heroService.getHero(+params['id']))
this.heroService.getHero(id) .subscribe(hero => this.hero = hero);
.then(hero => this.hero = hero);
});
} }
// #docregion save // #docregion save

View File

@ -445,7 +445,7 @@ h4#define-routes Define routes
:marked :marked
Adding the configured `RouterModule` to the `AppModule` is sufficient for simple route configurations. Adding the configured `RouterModule` to the `AppModule` is sufficient for simple route configurations.
As our application grows, we'll want to refactor our routing configuration into a separate file As our application grows, we'll want to refactor our routing configuration into a separate file
and create a **[Routing Module](#routing-module)**, a special type of `Service Module` dedicating for the purpose and create a **[Routing Module](#routing-module)**, a special type of `Service Module` dedicated for the purpose
of routing in feature modules. of routing in feature modules.
:marked :marked
@ -874,6 +874,11 @@ h3#activated-route ActivatedRoute: the one-stop-shop for route information
+makeExcerpt('app/heroes/hero-detail.component.1.ts (activated route)', 'imports') +makeExcerpt('app/heroes/hero-detail.component.1.ts (activated route)', 'imports')
:marked
We import the `switchMap` operator because we need it later to process the `Observable` route parameters.
+makeExcerpt('app/heroes/hero-detail.component.1.ts (switchMap operator import)', 'rxjs-operator-import')
a#hero-detail-ctor a#hero-detail-ctor
:marked :marked
As usual, we write a constructor that asks Angular to inject services As usual, we write a constructor that asks Angular to inject services
@ -882,13 +887,22 @@ a#hero-detail-ctor
+makeExcerpt('app/heroes/hero-detail.component.ts (constructor)', 'ctor') +makeExcerpt('app/heroes/hero-detail.component.ts (constructor)', 'ctor')
:marked :marked
Later, in the `ngOnInit` method, Later, in the `ngOnInit` method, we use the `ActivatedRoute` service to retrieve the parameters
we use the `ActivatedRoute` service to retrieve the parameters for our route. for our route. Since our parameters are provided as an `Observable`, we use the _switchMap_ operator to
Since our parameters are provided as an `Observable`, we use the _forEach_ method to retrieve them for the `id` parameter by name and provide them for the `id` parameter by name and tell the `HeroService` to fetch the hero with that `id`.
tell the `HeroService` to fetch the hero with that `id`.
+makeExcerpt('app/heroes/hero-detail.component.ts (ngOnInit)', 'ngOnInit') +makeExcerpt('app/heroes/hero-detail.component.ts (ngOnInit)', 'ngOnInit')
.l-sub-section
:marked
The `switchMap` operator allows you to perform an action with the current value of the `Observable`,
and map it to a new `Observable`. As with many `rxjs` operators, `switchMap` handles
an `Observable` as well as a `Promise` to retrieve the value they emit.
The `switchMap` operator will also cancel any in-flight requests if our user re-navigates to the route
while still retrieving a hero. Our `Observable` is _cold_ until subscribed to, so we use the `subscribe` method
to get and set our retrieved `Hero`.
.l-sub-section .l-sub-section
:marked :marked
Angular calls the `ngOnInit` method shortly after creating an instance of the `HeroDetailComponent`. Angular calls the `ngOnInit` method shortly after creating an instance of the `HeroDetailComponent`.
@ -1096,10 +1110,20 @@ code-example(language="bash").
+makeExcerpt('app/heroes/hero-list.component.ts (import)', 'import-router') +makeExcerpt('app/heroes/hero-list.component.ts (import)', 'import-router')
:marked :marked
Then we use the `ActivatedRoute` to access the `params` _Observable_ so we can subscribe We'll import the `switchMap` operator to perform an operation on our `Observable`
and extract the `id` parameter as the `selectedId`: of route parameters.
+makeExcerpt('app/heroes/hero-list.component.ts (constructor)', 'ctor') +makeExcerpt('app/heroes/hero-list.component.ts (rxjs imports)', 'rxjs-imports')
:marked
Then we inject the `ActivatedRoute` in the `HeroListComponent` constructor.
+makeExcerpt('app/heroes/hero-list.component.ts (constructor and ngOnInit)', 'ctor')
:marked
The ActivatedRoute.params property is an Observable of route parameters. The params emits new id values
when the user navigates to the component. In ngOnInit we subscribe to those values, set the selectedId,
and get the heroes.
.l-sub-section .l-sub-section
:marked :marked

View File

@ -511,6 +511,11 @@ block route-params
+makeExcerpt('app/hero-detail.component.ts (constructor)', 'ctor') +makeExcerpt('app/hero-detail.component.ts (constructor)', 'ctor')
:marked
We'll also import the `switchMap` operator to use later with the route parameters `Observable`.
+makeExcerpt('app/hero-detail.component.ts (switchMap import)', 'rxjs-import')
:marked :marked
We tell the class that we want to implement the `OnInit` interface. We tell the class that we want to implement the `OnInit` interface.
@ -526,14 +531,26 @@ block ngOnInit
block extract-id block extract-id
:marked :marked
Notice how we extract the `id` by calling the `forEach` method Note how the `switchMap` operator maps the id in the observable route parameters
which will deliver our !{_array} of route parameters. to a new `Observable`, the result of the `HeroService.getHero` method.
If the user re-navigates to this component while a getHero request is still inflight,
switchMap cancels that old request before calling `HeroService.getHero` again.
- var _str2int = _docsFor == 'dart' ? '<code>int.parse</code> static method' : 'JavaScript (+) operator' - var _str2int = _docsFor == 'dart' ? '<code>int.parse</code> static method' : 'JavaScript (+) operator'
:marked :marked
The hero `id` is a number. Route parameters are *always strings*. The hero `id` is a number. Route parameters are *always strings*.
So we convert the route parameter value to a number with the !{_str2int}. So we convert the route parameter value to a number with the !{_str2int}.
.l-sub-section
:marked
### Do I need to unsubscribe?
The `Router` manages the [observables](../guide/router.html#activated-route) it provides and localizes
the subscriptions. The subscriptions are cleaned up when the component is destroyed, protecting against
memory leaks, so we don't need to _unsubscribe_ from the route params `Observable`.
:marked
### Add *HeroService.getHero* ### Add *HeroService.getHero*
The problem with this bit of code is that `HeroService` doesn't have a `getHero` method! The problem with this bit of code is that `HeroService` doesn't have a `getHero` method!
@ -654,16 +671,16 @@ block extract-id
+makeExample('app/app-routing.module.ts') +makeExample('app/app-routing.module.ts')
:marked :marked
Noteworthy points, typical of _Routing Modules_: Noteworthy points, typical of _Routing Modules_:
* Pull the routes into a variable. You might export it in future and it clarifies the _Routing Module_ pattern. * Pulls the routes into a variable. You might export it in future and it clarifies the _Routing Module_ pattern.
* Add `RouterModule.forRoot(routes)` to `imports`. * Adds `RouterModule.forRoot(routes)` to `imports`.
* Add `RouterModule` to `exports` so that the components in the companion module have access to Router declarables * Adds `RouterModule` to `exports` so that the components in the companion module have access to Router declarables
such as `RouterLink` and `RouterOutlet`. such as `RouterLink` and `RouterOutlet`.
* No `declarations`! Declarations are the responsibility of the companion module. * No `declarations`! Declarations are the responsibility of the companion module.
* Add module `providers` for guard services if you have them; there are none in this example. * Adds module `providers` for guard services if you have them; there are none in this example.
### Update _AppModule_ ### Update _AppModule_