docs(toh-6/ts): Upgraded http tutorial to use new router

This commit is contained in:
Brandon Roberts 2016-06-26 12:13:44 -05:00 committed by Naomi Black
parent b049c1bcf4
commit 97fbda0d76
8 changed files with 73 additions and 39 deletions

View File

@ -24,6 +24,6 @@ nav a:hover {
color: #039be5; color: #039be5;
background-color: #CFD8DC; background-color: #CFD8DC;
} }
nav a.router-link-active { nav a.active {
color: #039be5; color: #039be5;
} }

View File

@ -1,12 +1,9 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { Component } from '@angular/core'; import { Component } from '@angular/core';
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated'; import { ROUTER_DIRECTIVES } from '@angular/router';
import { DashboardComponent } from './dashboard.component'; import { HeroService } from './hero.service';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroService } from './hero.service';
@Component({ @Component({
selector: 'my-app', selector: 'my-app',
@ -14,23 +11,17 @@ import { HeroService } from './hero.service';
template: ` template: `
<h1>{{title}}</h1> <h1>{{title}}</h1>
<nav> <nav>
<a [routerLink]="['Dashboard']">Dashboard</a> <a [routerLink]="['/dashboard']" routerLinkActive="active">Dashboard</a>
<a [routerLink]="['Heroes']">Heroes</a> <a [routerLink]="['/heroes']" routerLinkActive="active">Heroes</a>
</nav> </nav>
<router-outlet></router-outlet> <router-outlet></router-outlet>
`, `,
styleUrls: ['app/app.component.css'], styleUrls: ['app/app.component.css'],
directives: [ROUTER_DIRECTIVES], directives: [ROUTER_DIRECTIVES],
providers: [ providers: [
ROUTER_PROVIDERS,
HeroService, HeroService,
] ]
}) })
@RouteConfig([
{ path: '/dashboard', name: 'Dashboard', component: DashboardComponent, useAsDefault: true },
{ path: '/detail/:id', name: 'HeroDetail', component: HeroDetailComponent },
{ path: '/heroes', name: 'Heroes', component: HeroesComponent }
])
export class AppComponent { export class AppComponent {
title = 'Tour of Heroes'; title = 'Tour of Heroes';
} }

View File

@ -0,0 +1,30 @@
// #docregion
import { provideRouter, RouterConfig } from '@angular/router';
import { DashboardComponent } from './dashboard.component';
import { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
export const routes: RouterConfig = [
{
path: '',
redirectTo: '/dashboard',
terminal: true
},
{
path: 'dashboard',
component: DashboardComponent
},
{
path: 'detail/:id',
component: HeroDetailComponent
},
{
path: 'heroes',
component: HeroesComponent
}
];
export const APP_ROUTER_PROVIDERS = [
provideRouter(routes)
];

View File

@ -1,7 +1,7 @@
// #docplaster // #docplaster
// #docregion // #docregion
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router-deprecated'; import { Router } from '@angular/router';
import { Hero } from './hero'; import { Hero } from './hero';
import { HeroService } from './hero.service'; import { HeroService } from './hero.service';
@ -26,7 +26,7 @@ export class DashboardComponent implements OnInit {
} }
gotoDetail(hero: Hero) { gotoDetail(hero: Hero) {
let link = ['HeroDetail', { id: hero.id }]; let link = ['/detail', hero.id];
this.router.navigate(link); this.router.navigate(link);
} }
} }

View File

@ -1,9 +1,9 @@
// #docplaster // #docplaster
// #docregion, variables-imports // #docregion, variables-imports
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { Component, EventEmitter, Input, OnInit, OnDestroy, Output } from '@angular/core';
// #enddocregion variables-imports // #enddocregion variables-imports
import { RouteParams } from '@angular/router-deprecated'; import { ActivatedRoute } from '@angular/router';
import { Hero } from './hero'; import { Hero } from './hero';
import { HeroService } from './hero.service'; import { HeroService } from './hero.service';
@ -14,31 +14,39 @@ import { HeroService } from './hero.service';
styleUrls: ['app/hero-detail.component.css'] styleUrls: ['app/hero-detail.component.css']
}) })
// #docregion variables-imports // #docregion variables-imports
export class HeroDetailComponent implements OnInit { export class HeroDetailComponent implements OnInit, OnDestroy {
@Input() hero: Hero; @Input() hero: Hero;
@Output() close = new EventEmitter(); @Output() close = new EventEmitter();
error: any; error: any;
sub: any;
navigated = false; // true if navigated here navigated = false; // true if navigated here
// #enddocregion variables-imports // #enddocregion variables-imports
constructor( constructor(
private heroService: HeroService, private heroService: HeroService,
private routeParams: RouteParams) { private route: ActivatedRoute) {
} }
// #docregion ngOnInit // #docregion ngOnInit
ngOnInit() { ngOnInit() {
if (this.routeParams.get('id') !== null) { this.sub = this.route.params.subscribe(params => {
let id = +this.routeParams.get('id'); if (params['id'] !== undefined) {
this.navigated = true; let id = +params['id'];
this.heroService.getHero(id) this.navigated = true;
.then(hero => this.hero = hero); this.heroService.getHero(id)
} else { .then(hero => this.hero = hero);
this.navigated = false; } else {
this.hero = new Hero(); this.navigated = false;
} this.hero = new Hero();
}
});
} }
// #enddocregion ngOnInit // #enddocregion ngOnInit
ngOnDestroy() {
this.sub.unsubscribe();
}
// #docregion save // #docregion save
save() { save() {
this.heroService this.heroService
@ -57,4 +65,3 @@ export class HeroDetailComponent implements OnInit {
} }
// #enddocregion goBack // #enddocregion goBack
} }

View File

@ -1,6 +1,6 @@
// #docregion // #docregion
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router-deprecated'; import { Router } from '@angular/router';
import { Hero } from './hero'; import { Hero } from './hero';
import { HeroService } from './hero.service'; import { HeroService } from './hero.service';
@ -68,6 +68,6 @@ export class HeroesComponent implements OnInit {
} }
gotoDetail() { gotoDetail() {
this.router.navigate(['HeroDetail', { id: this.selectedHero.id }]); this.router.navigate(['/detail', this.selectedHero.id]);
} }
} }

View File

@ -11,16 +11,21 @@ import { InMemoryDataService } from './in-memory-data.service';
import { bootstrap } from '@angular/platform-browser-dynamic'; import { bootstrap } from '@angular/platform-browser-dynamic';
import { HTTP_PROVIDERS } from '@angular/http'; import { HTTP_PROVIDERS } from '@angular/http';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
import { APP_ROUTER_PROVIDERS } from './app.routes';
// #enddocregion v1, final // #enddocregion v1, final
/* /*
// #docregion v1 // #docregion v1
bootstrap(AppComponent, [ HTTP_PROVIDERS ]); bootstrap(AppComponent, [
APP_ROUTER_PROVIDERS,
HTTP_PROVIDERS
]);
// #enddocregion v1 // #enddocregion v1
*/ */
// #docregion final // #docregion final
bootstrap(AppComponent, [ bootstrap(AppComponent, [
APP_ROUTER_PROVIDERS,
HTTP_PROVIDERS, HTTP_PROVIDERS,
{ provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server { provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
{ provide: SEED_DATA, useClass: InMemoryDataService } // in-mem server data { provide: SEED_DATA, useClass: InMemoryDataService } // in-mem server data

View File

@ -7,7 +7,7 @@ block includes
- var _Angular_http_library = 'Angular HTTP library' - var _Angular_http_library = 'Angular HTTP library'
- var _HTTP_PROVIDERS = 'HTTP_PROVIDERS' - var _HTTP_PROVIDERS = 'HTTP_PROVIDERS'
- var _JSON_stringify = 'JSON.stringify' - var _JSON_stringify = 'JSON.stringify'
:marked :marked
# Getting and Saving Data with HTTP # Getting and Saving Data with HTTP
@ -250,8 +250,8 @@ block hero-detail-comp-updates
:marked :marked
### Add/Edit in the *HeroDetailComponent* ### Add/Edit in the *HeroDetailComponent*
We already have `HeroDetailComponent` for viewing details about a specific hero. We already have `HeroDetailComponent` for viewing details about a specific hero.
Add and Edit are natural extensions of the detail view, so we are able to reuse `HeroDetailComponent` with a few tweaks. Add and Edit are natural extensions of the detail view, so we are able to reuse `HeroDetailComponent` with a few tweaks.
The original component was created to render existing data, but to add new data we have to initialize the `hero` property to an empty `Hero` object. The original component was created to render existing data, but to add new data we have to initialize the `hero` property to an empty `Hero` object.
@ -373,6 +373,7 @@ block filetree
.children .children
.file app.component.ts .file app.component.ts
.file app.component.css .file app.component.css
.file app.routes.ts
.file dashboard.component.css .file dashboard.component.css
.file dashboard.component.html .file dashboard.component.html
.file dashboard.component.ts .file dashboard.component.ts