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

Commit 97fbda0

Browse files
brandonrobertsnaomiblack
authored andcommitted
docs(toh-6/ts): Upgraded http tutorial to use new router
1 parent b049c1b commit 97fbda0

File tree

8 files changed

+73
-39
lines changed

8 files changed

+73
-39
lines changed

public/docs/_examples/toh-6/ts/app/app.component.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ nav a:hover {
2424
color: #039be5;
2525
background-color: #CFD8DC;
2626
}
27-
nav a.router-link-active {
27+
nav a.active {
2828
color: #039be5;
2929
}
Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
11
// #docplaster
22
// #docregion
3-
import { Component } from '@angular/core';
4-
import { RouteConfig, ROUTER_DIRECTIVES, ROUTER_PROVIDERS } from '@angular/router-deprecated';
3+
import { Component } from '@angular/core';
4+
import { ROUTER_DIRECTIVES } from '@angular/router';
55

6-
import { DashboardComponent } from './dashboard.component';
7-
import { HeroesComponent } from './heroes.component';
8-
import { HeroDetailComponent } from './hero-detail.component';
9-
import { HeroService } from './hero.service';
6+
import { HeroService } from './hero.service';
107

118
@Component({
129
selector: 'my-app',
1310

1411
template: `
1512
<h1>{{title}}</h1>
1613
<nav>
17-
<a [routerLink]="['Dashboard']">Dashboard</a>
18-
<a [routerLink]="['Heroes']">Heroes</a>
14+
<a [routerLink]="['/dashboard']" routerLinkActive="active">Dashboard</a>
15+
<a [routerLink]="['/heroes']" routerLinkActive="active">Heroes</a>
1916
</nav>
2017
<router-outlet></router-outlet>
2118
`,
2219
styleUrls: ['app/app.component.css'],
2320
directives: [ROUTER_DIRECTIVES],
2421
providers: [
25-
ROUTER_PROVIDERS,
2622
HeroService,
2723
]
2824
})
29-
@RouteConfig([
30-
{ path: '/dashboard', name: 'Dashboard', component: DashboardComponent, useAsDefault: true },
31-
{ path: '/detail/:id', name: 'HeroDetail', component: HeroDetailComponent },
32-
{ path: '/heroes', name: 'Heroes', component: HeroesComponent }
33-
])
3425
export class AppComponent {
3526
title = 'Tour of Heroes';
3627
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// #docregion
2+
import { provideRouter, RouterConfig } 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+
export const routes: RouterConfig = [
9+
{
10+
path: '',
11+
redirectTo: '/dashboard',
12+
terminal: true
13+
},
14+
{
15+
path: 'dashboard',
16+
component: DashboardComponent
17+
},
18+
{
19+
path: 'detail/:id',
20+
component: HeroDetailComponent
21+
},
22+
{
23+
path: 'heroes',
24+
component: HeroesComponent
25+
}
26+
];
27+
28+
export const APP_ROUTER_PROVIDERS = [
29+
provideRouter(routes)
30+
];

public/docs/_examples/toh-6/ts/app/dashboard.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// #docplaster
22
// #docregion
33
import { Component, OnInit } from '@angular/core';
4-
import { Router } from '@angular/router-deprecated';
4+
import { Router } from '@angular/router';
55

66
import { Hero } from './hero';
77
import { HeroService } from './hero.service';
@@ -26,7 +26,7 @@ export class DashboardComponent implements OnInit {
2626
}
2727

2828
gotoDetail(hero: Hero) {
29-
let link = ['HeroDetail', { id: hero.id }];
29+
let link = ['/detail', hero.id];
3030
this.router.navigate(link);
3131
}
3232
}

public/docs/_examples/toh-6/ts/app/hero-detail.component.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// #docplaster
22
// #docregion, variables-imports
3-
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
3+
import { Component, EventEmitter, Input, OnInit, OnDestroy, Output } from '@angular/core';
44

55
// #enddocregion variables-imports
6-
import { RouteParams } from '@angular/router-deprecated';
6+
import { ActivatedRoute } from '@angular/router';
77

88
import { Hero } from './hero';
99
import { HeroService } from './hero.service';
@@ -14,31 +14,39 @@ import { HeroService } from './hero.service';
1414
styleUrls: ['app/hero-detail.component.css']
1515
})
1616
// #docregion variables-imports
17-
export class HeroDetailComponent implements OnInit {
17+
export class HeroDetailComponent implements OnInit, OnDestroy {
1818
@Input() hero: Hero;
1919
@Output() close = new EventEmitter();
2020
error: any;
21+
sub: any;
2122
navigated = false; // true if navigated here
2223
// #enddocregion variables-imports
2324

2425
constructor(
2526
private heroService: HeroService,
26-
private routeParams: RouteParams) {
27+
private route: ActivatedRoute) {
2728
}
2829

2930
// #docregion ngOnInit
3031
ngOnInit() {
31-
if (this.routeParams.get('id') !== null) {
32-
let id = +this.routeParams.get('id');
33-
this.navigated = true;
34-
this.heroService.getHero(id)
35-
.then(hero => this.hero = hero);
36-
} else {
37-
this.navigated = false;
38-
this.hero = new Hero();
39-
}
32+
this.sub = this.route.params.subscribe(params => {
33+
if (params['id'] !== undefined) {
34+
let id = +params['id'];
35+
this.navigated = true;
36+
this.heroService.getHero(id)
37+
.then(hero => this.hero = hero);
38+
} else {
39+
this.navigated = false;
40+
this.hero = new Hero();
41+
}
42+
});
4043
}
4144
// #enddocregion ngOnInit
45+
46+
ngOnDestroy() {
47+
this.sub.unsubscribe();
48+
}
49+
4250
// #docregion save
4351
save() {
4452
this.heroService
@@ -57,4 +65,3 @@ export class HeroDetailComponent implements OnInit {
5765
}
5866
// #enddocregion goBack
5967
}
60-

public/docs/_examples/toh-6/ts/app/heroes.component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// #docregion
22
import { Component, OnInit } from '@angular/core';
3-
import { Router } from '@angular/router-deprecated';
3+
import { Router } from '@angular/router';
44

55
import { Hero } from './hero';
66
import { HeroService } from './hero.service';
@@ -68,6 +68,6 @@ export class HeroesComponent implements OnInit {
6868
}
6969

7070
gotoDetail() {
71-
this.router.navigate(['HeroDetail', { id: this.selectedHero.id }]);
71+
this.router.navigate(['/detail', this.selectedHero.id]);
7272
}
7373
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@ import { InMemoryDataService } from './in-memory-data.service';
1111
import { bootstrap } from '@angular/platform-browser-dynamic';
1212
import { HTTP_PROVIDERS } from '@angular/http';
1313

14-
import { AppComponent } from './app.component';
14+
import { AppComponent } from './app.component';
15+
import { APP_ROUTER_PROVIDERS } from './app.routes';
1516

1617
// #enddocregion v1, final
1718
/*
1819
// #docregion v1
19-
bootstrap(AppComponent, [ HTTP_PROVIDERS ]);
20+
bootstrap(AppComponent, [
21+
APP_ROUTER_PROVIDERS,
22+
HTTP_PROVIDERS
23+
]);
2024
// #enddocregion v1
21-
*/
25+
*/
2226
// #docregion final
2327
bootstrap(AppComponent, [
28+
APP_ROUTER_PROVIDERS,
2429
HTTP_PROVIDERS,
2530
{ provide: XHRBackend, useClass: InMemoryBackendService }, // in-mem server
2631
{ provide: SEED_DATA, useClass: InMemoryDataService } // in-mem server data

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ block includes
77
- var _Angular_http_library = 'Angular HTTP library'
88
- var _HTTP_PROVIDERS = 'HTTP_PROVIDERS'
99
- var _JSON_stringify = 'JSON.stringify'
10-
10+
1111
:marked
1212
# Getting and Saving Data with HTTP
1313

@@ -250,8 +250,8 @@ block hero-detail-comp-updates
250250
:marked
251251
### Add/Edit in the *HeroDetailComponent*
252252

253-
We already have `HeroDetailComponent` for viewing details about a specific hero.
254-
Add and Edit are natural extensions of the detail view, so we are able to reuse `HeroDetailComponent` with a few tweaks.
253+
We already have `HeroDetailComponent` for viewing details about a specific hero.
254+
Add and Edit are natural extensions of the detail view, so we are able to reuse `HeroDetailComponent` with a few tweaks.
255255

256256
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.
257257

@@ -373,6 +373,7 @@ block filetree
373373
.children
374374
.file app.component.ts
375375
.file app.component.css
376+
.file app.routes.ts
376377
.file dashboard.component.css
377378
.file dashboard.component.html
378379
.file dashboard.component.ts

0 commit comments

Comments
 (0)