【发布时间】:2018-08-31 12:11:37
【问题描述】:
谁能帮忙,,
所以我使用@ngx-pwa 将登录数据存储在称为登录的本地存储密钥中
我正在尝试获取此数据并显示它,但我无法定义!
public customerProfile
ngOnInit() {
this.getProfileData();
console.log(this.cutomerProfile) //shows undefined
}
getProfileData() {
this.localStorage.getItem('login').subscribe((login) => {
this.customerProfile = login;
console.log(this.customerProfile.user) //shows login data
})
}
【问题讨论】:
-
那是因为
getItem是异步的,所以ngOnInit试图在设置之前打印customerProfile。 -
所以我应该使用 afterViewInit() {} 还是我应该怎么做才能让它正确
标签: javascript angular typescript angular5