微信小程序中setData的问题

就比如说我在小程序里写了一个公共的弹层,点击其中一个弹层会显示对应的内容

js部分如下

layerTap(e){
    this.setData({
      index: this.data.expert[e.currentTarget.dataset.index],
      expertDetail: true//显示弹层
    })
}

如果我用箭头函数,就会报【Cannot read property 'setData' of undefined;at "page/discover/discover" page bindViewTap function

TypeError: Cannot read property 'setData' of undefined】代码如下

layerTap:(e)=>{
    this.setData({
      index: this.data.expert[e.currentTarget.dataset.index],
      expertDetail: true//显示弹层
    })
}

然而我在onLoad里var that = this然后再setData,依旧会报错【

that is not defined;at "page/discover/discover" page bindViewTap function

ReferenceError: that is not defined


为什么,还是说我只要遇到需要使用setData的情况就不能使用箭头函数?

安静的沙漠
浏览 10731回答 3
3回答

Geek丶

十分感谢,找了好久的BUG,箭头函数不能操作Page的data,

若小点

不应该在onload里面声明var that = this; 因为onload本身就是一个作用域! 应该在你写的函数里吗 声明

考拉2

...
打开App,查看更多内容
随时随地看视频慕课网APP