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,11 +1,8 @@
// #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 { HeroesComponent } from './heroes.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroService } from './hero.service'; import { HeroService } from './hero.service';
@Component({ @Component({
@ -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,22 +14,24 @@ 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) {
let id = +params['id'];
this.navigated = true; this.navigated = true;
this.heroService.getHero(id) this.heroService.getHero(id)
.then(hero => this.hero = hero); .then(hero => this.hero = hero);
@ -37,8 +39,14 @@ export class HeroDetailComponent implements OnInit {
this.navigated = false; this.navigated = false;
this.hero = new Hero(); 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

@ -12,15 +12,20 @@ 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

@ -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