我的 api 使用数字而不是字符串 id。如果我想这样做,我会收到错误:无法将类型字符串分配给类型编号。该param函数是否仅将字符串作为 id 或如何将其更改为数字?
这是我的代码:
用户服务.ts
// get a user's profile
getUserDetails(id: number): Observable<any> {
return this.http.get(`${this.url}/users/${id}/`);
}
user.page.html
<ion-avatar class="user-image" slot="start" [routerLink]="['/', 'friendsdet', 'id']">
<ion-img src="assets/9.jpeg"> </ion-img>
</ion-avatar>
Friendsdet.page.ts
information: null;
id: number;
...
ngOnInit() {
// Get the ID that was passed with the URL
this.activatedRoute.paramMap.subscribe(params => {
this.id = params.get('id');
});
// Get the information from the API
this.userService.getUserDetails(this.id).subscribe(result => {
this.information = result;
console.log(result);
});
}
应用程序路由.module.ts
...
{ path: 'friendsdet/:id',
loadChildren: './external-user/friendsdet/friendsdet.module#FriendsdetPageModule'
},
...