由於疫情原因目前處於半下崗狀態,在家的時候就研究起了小程序開發。由於是新手,所以總會遇到各種問題,順便記錄一下。
wx.showModal({ title: '提示', content: '這是一個模態彈窗', success (res) { if (res.confirm) { this.data.messageId } else if (res.cancel) { console.log('用戶點擊取消') } } })
如果這么寫,會遇到“Cannot read property 'data' of undefined ”的錯誤。
如果改成箭頭函數的寫法,就不會出錯。
wx.showModal({ title: '提示', content: '這是一個模態彈窗', success :(res)=> { if (res.confirm) { this.data.messageId } else if (res.cancel) { console.log('用戶點擊取消') } } })
分析原因,箭頭函數是ES6的寫法,不同的寫法,會導致“this”的指代層級不同,所以會找不到data屬性。