onLoad (options) {
wx.showModal({
success(){
console.log(this)//undefined
this.setData({
postList
})
}
})
}
如上述代码,直接在回填函数里面写this.setData是会报错的,原因是此时在回填函数里面this返回的值是undefined;
解决方案有2种:
1.使用that指代this:
onLoad (options) {
let that = this
wx.showModal({
success(){
console.log(that)
that.setData({
postList
})
}
})
}
2.使用箭头函数(推荐使用箭头函数):
onLoad (options) {
wx.showModal({
success:()=>{
console.log(this)
this.setData({
postList
})
}
})
}