我正在做Angular项目。我想获取我单击的一条记录的 Id,以传入另一个组件。
当我单击记录时,我能够在 URL 中看到 ID,但无法在新组件中获取。
这是我的第一个组件的代码
<tr><td> <a routerLink="/devicelist/{{lst.ID.Value}}">{{lst.Failed}}</a> </td></tr>
在第二个组件中的代码下方获取 ID,但在将id
附加到 url 时,它显示未定义。
第二补偿代码
id = this.actRoute.snapshot.params['ID.Value'];
ngOnInit() {
this.getFailedApps(this.id).subscribe((data:any) => {
this.devices = data;
console.log(data);
})
}
public getFailedApps(Id) {
//here Id is Undefined
return this._http.get(this.FailedDeviceUrl +Id+"/myapps", this.httpOptions);
}
这是我的路由文件
{ path: 'devicelist/:id', component: DeviceListComponent }
用以下代码替换您的路由器链接
<tr><td> <a [routerLink]="['/devicelist', lst.ID]" >{{lst.Failed}}</a> </td></tr>
同时在 AppModule.ts 中的路由中声明参数
{ path: 'devicelist/:id', component: YourComponent }
然后在组件中使用以下代码读取参数
id: number;
constructor(private route: ActivatedRoute) {}
ngOnInit() {
this.sub = this.route.params.subscribe(params => {
this.id = +params['id']; // (+) converts string 'id' to a number
});
}