【发布时间】:2021-03-20 09:09:55
【问题描述】:
我收到一个错误ERROR TypeError: Cannot read property 'name' of undefined
我有一个从存储中检索数据的服务和一个调用该服务并显示数据的主页。
storage.service.ts
async initilizeData() {
this.data = await this.storage.get('myData')
}
getData() : MyData {
return this.data
}
home.page.ts
data: MyData
async ngOnInit() {
this.storageService.initializeData()
this.data = this.storageService.getData()
console.log(this.data)
}
home.page.html
<ion-text>{{data.name}}</ion-text>
<ion-text>{{data.amount}}</ion-text>
ngOnInit() 中的 console.log() 确实为我注销了数据,但在 HTML 页面中 data.name 和 data.amount 似乎都未定义(我知道,当我将 *ngIf="!data == null" 放在代码周围时,错误消失了。)
谢谢大家的回答:)
【问题讨论】: