#報錯1悔政,Error message:Cannot read property resultData of undefined
舉例如下:
@State resultData: ResultDataSource = new ResultDataSource()
aboutToAppear(): void {
emitter.on({ eventId: 1 }, this.fabClickEvent)
}
fabClickEvent() {
this.resultData.pushData(new IndexItem('11','12'))
}
分析:這代碼很簡單眠蚂,但是卻訪問不到this,原因是:回調嵌套了多層,this其實指向的并不是當前頁面
解決方案:
aboutToAppear(): void {
emitter.on({ eventId: 1 }, this.fabClickEvent.bind(this))
}
使用bind把this傳給你要使用的地方即可