参考来源:https://www.angular.cn/tutorial/toh-pt5#add-dashboard-link-to-the-shell
1、配置路由模块app\app-routing.module.ts
import {NgModule} from '@angular/core';
import {RouterModule, Routes} from '@angular/router';
import {HeroesComponent} from './heroes/heroes.component';
import {DashboardComponent} from './dashboard/dashboard.component';
import {HeroDetailComponent} from './hero-detail/hero-detail.component';
const routes: Routes = [
{path: '', redirectTo: '/dashboard', pathMatch: 'full'},
{path: 'heroes', component: HeroesComponent},
{path: 'dashboard', component: DashboardComponent},
{path: 'detail/:id', component: HeroDetailComponent}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {
}
2、使用路由app\app.component.html
<h1>{{title}}</h1>
<nav>
<a routerLink="/dashboard">Dashboard</a>
<a routerLink="/heroes">Heroes</a>
</nav>
<router-outlet></router-outlet>
<app-messages></app-messages>
3、英雄服务app\service\hero.service.ts
import {Injectable} from '@angular/core';
import {Hero} from '../hero';
import {HEROES} from '../mock-heroes';
import {Observable, of} from 'rxjs';
import {MessageService} from './message.service';
@Injectable()
export class HeroService {
constructor(private messageService: MessageService) {
}
getHeroes(): Observable<Hero[]> {
this.messageService.add('HeroService: fetched heroes');
return of(HEROES);
}
getHero(id: number): Observable<Hero> {
this.messageService.add(`HeroService: frtched hero id ${id}`);
return of(HEROES.find(hero => hero.id === id));
}
}
4、英雄列表模板app\heroes\heroes.component.html
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor="let hero of heroes">
<a routerLink="/detail/{{hero.id}}">
<span class="badge">{{hero.id}}</span>{{hero.name}}
</a>
</li>
</ul>
5、英雄仪表盘模板app\dashboard\dashboard.component.html
<h3>Top Heroes</h3>
<div class="grid grid-pad">
<a *ngFor="let hero of heroes" class="col-1-4"
routerLink="/detail/{{hero.id}}">
<div class="module.hero">
<h4>{{hero.name}}</h4>
</div>
</a>
</div>
6、英雄详情模板app\hero-detail\hero-detail.component.html
<div *ngIf="hero"> <h2>{{ hero.name | uppercase }} Detail </h2> <div><span>id:</span>{{hero.id}}</div> <div> <label>name: <input [(ngModel)]="hero.name" placeholder="name"> </label> </div> </div> <button (click)="goBack()">go back</button>
7、英雄详情组件app\hero-detail\hero-detail.component.ts
import {Component, OnInit} from '@angular/core';
import {Hero} from '../hero';
import {ActivatedRoute} from '@angular/router';
import {HeroService} from '../service/hero.service';
import {Location} from '@angular/common';
@Component({
selector: 'app-hero-detail',
templateUrl: './hero-detail.component.html',
})
export class HeroDetailComponent implements OnInit {
hero: Hero;
constructor(
private route: ActivatedRoute,
private heroService: HeroService,
private location: Location
) {
}
ngOnInit(): void {
this.getHero();
}
getHero(): void {
const id = +this.route.snapshot.paramMap.get('id');
this.heroService.getHero(id).subscribe(data => {
this.hero = data;
});
}
goBack(): void {
this.location.back();
}
}