如何从登录用户获取详细信息

时间:2018-10-24 22:58:25

标签: angular typescript jwt

我想从本地存储中检索记录的用户信息,以便在用户对应用程序进行身份验证后向其打招呼。

login(user){
    localStorage.setItem('currentUser',  JSON.stringify(user));
    return this.http.post(this.host+"/login",user, {observe:'response'});
}


export class HomeComponent implements OnInit {
  currentUser:User;

  constructor(public authenticationService:AuthenticationService) {
    this.currentUser= JSON.parse(localStorage.getItem('currentUser'));
    console.log(this.currentUser);
}

在这里,我从本地存储呼叫用户,但是它不起作用。你能帮我吗?

<div class="alert alert-dark" role="alert">
    <h2>Welcome {{currentUser.firstName}} !!</h2>
</div>

1 个答案:

答案 0 :(得分:0)

在这一行:

this.currentUser = JSON.parse(localStorage.getItem('currentUer'));

您得到currentUer而不是currentUser

您是否在devtools中查看该密钥?