【发布时间】:2019-11-12 13:39:38
【问题描述】:
我想在我的应用中调用当前用户的个人资料。为了做到这一点,我将用户存储在登录过程中。我将值 user_id 存储在我的 auth.service 中。然后我尝试在配置文件页面中获取此键值,以显示登录的用户数据。但我真的不知道如何使用这个 ID。由于我不是来自另一个页面,我无法使用 activateRoute 逻辑。我以为我只需要调用存储然后将值存储在我的information 变量中。存储 USER_ID 的 id 是一个数字,并且存储正在工作我在那里看到我的 id。如果我想在个人资料页面中调用我的 id,我会收到 patchValue is not available for type number 的错误。
auth.service.ts
export const USER_ID = 'user_id';
...
this.user = this.helper.decodeToken(res['access']); // the user id is stored value = 1
user.service.ts
// get a user's profile
getUserDetails(id: number): Observable<any> {
return this.http.get(`${this.url}/users/${id}/`);
}
profile.page.ts
information = null; // store my data here
id: number; // Id to get the user data from server is a number
...
ngOnInit() {
this.storage.get(USER_ID).then(val => { // get the stored user_id
this.id.patchValue({ user_id: val}); // store the current userid in id
console.log(this.id);
});
// Get the information from the API
this.userService.getUserDetails(this.id).subscribe(result => { // get the user data from the server
this.information = result;
console.log(result);
console.log(this.information);
console.log(this.id);
});
}
【问题讨论】:
标签: javascript angular typescript ionic-framework