Pass Verbindung zum Router:
<a [routerLink]="['/Person', person.id]">Person</a>
Griff in der Komponente :
this.route.params.subscribe(
(params : Params) => {
this.id = params["id"];
}
);
Zweiter Weg:
this.route.params.forEach(
(params : Params) => {
this.id = params["id"];
}
);
In diesem Beispiel Sie ActivatedRoute
(zB als Route Eigenschaft) so zu injizieren haben:
constructor(private route : ActivatedRoute) {}
Wenn Sie sich vorher anmelden - es ist wichtig zu stornieren Observable
zu verhindern Speicherlecks.
Voll Beispiel:
export class SimpleComponent implements OnInit, OnDestroy {
private id : number;
private route$ : Subscription;
constructor(private route : ActivatedRoute) {}
ngOnInit() {
this.route$ = this.route.params.subscribe(
(params : Params) => {
this.id = +params["id"]; // cast to number
}
);
}
ngOnDestroy() {
if(this.route$) this.route$.unsubscribe();
}
}
Config:
export const routes = [
...
{ path : 'Person/:id', component : ...},
...
];
Auch @RouteConfig
ist veraltet.
Es gibt keine '@ RouteConfig'. Welche Angular2 Version verwendest du? –