下面是如何为路由参数进行 RouteConfig
{path: '/hero/:id', name: 'HeroDetail', component: HeroDetailComponent},
这是来自已配置应用程序的代码,其中包含路由和路由参数
@Component({
selector: 'my-app',
template: `
<h1 class="title">Component Router</h1>
<a [routerLink]="['CrisisCenter']">Crisis Center</a>
<a [routerLink]="['Heroes']">Heroes</a>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{ // Crisis Center child route
path: '/crisis-center/...',
name: 'CrisisCenter',
component: CrisisCenterComponent,
useAsDefault: true
},
{path: '/heroes', name: 'Heroes', component: HeroListComponent},
{path: '/hero/:id', name: 'HeroDetail', component: HeroDetailComponent},
{path: '/disaster', name: 'Asteroid', redirectTo: ['CrisisCenter', 'CrisisDetail', {id:3}]}
])
export class AppComponent {
constructor(private router: Router) {
this.setUpEvents();
}
private setUpEvents(): void {
this.router.subscribe((value: any) => this.onNext(value));
}
private onNext(value: any): void {
//uncomment to get the stacktrace
//throw new Exception("");
console.log(value);
}
}
该示例的 plunker
在完整视图中,您可以看到路由和路由参数如何更改以显示新内容: 单击此处