我有一个< a>标签如下,
<a [routerLink]="[/Person']">Person</a>
所以我想把person.id传递给这个/ Person路由器.我怎样才能通过它&在@RouteConfig上处理它,就像Angular 1中的params一样
传递给路由器链接:
原文链接:https://www.f2er.com/angularjs/143474.html<a [routerLink]="['/Person',person.id]">Person</a>
处理组件:
this.route.params.subscribe( (params : Params) => { this.id = params["id"]; } );
第二种方式:
this.route.params.forEach( (params : Params) => { this.id = params["id"]; } );
在此示例中,您必须像这样注入ActivatedRoute(例如路由属性):
constructor(private route : ActivatedRoute) {}
如果您订阅 – 取消订阅Observable以防止内存泄漏非常重要.
完整示例:
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(); } }
配置:
export const routes = [ ... { path : 'Person/:id',component : ...},... ];
此外,@ RouteConfig已弃用.