1 回答
TA贡献1865条经验 获得超7个赞
更新的答案 您需要user_id像这样将您的内容保存到存储中
this.storage.set(USER_ID, this.validateId(res['user_id']))
并在当前用户配置文件组件中获取此信息user_id并将其传递给如下this.userService.getUserDetails方法:
const currentUserId = this.storage.get(USER_ID);
this.userService.getUserDetails(currentUserId).subscribe(user => {
console.log('user data', user);
});
旧答案 假设任何用户的配置文件都在同一条路线上,(ex: profile/:userId)包括当前经过身份验证的用户
您只需要在订阅中添加此调用activatedRoute.paramMap,
尝试
ngOnInit() {
// How to get just the authenticated api?
this.activatedRoute.paramMap.subscribe(params => {
this.id = validateId(params.get('id'));
// Get the information from the API
this.userService.getUserDetails(this.id).subscribe(result => {
this.information = result;
});
});
function validateId(id: any): number {
return parseInt(id);
}
}
让我解释一下发生了什么,当您进入该组件时,ngOnInit会调用钩子并通过 id 获取用户,并且在您尝试打开当前已验证用户后,仅调用activatedRoute.paramMap订阅的回调而不是所有ngOnInit方法。
添加回答
举报
