微信小程序 this.setData is not a function

在一般的函数中:

  bindFaChange1: function (e) {
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
      index1: e.detail.value
    })
  }

this.setData是正确的。

但当在函数中有个请求(wx.request)时:

复制代码
formSubmit: function (e) {
    wx.request({
      method: 'POST',
      header: header,
      url: url,
      dataType: 'json',
     success: function (res) {
           this.setData({
              data1: true
            })
      }
    })
}    
复制代码

这样会报错误:this.setData is not a function.

解决方法就是 :在请求(wx.request)外面添加:var that=this;将success中的

           this.setData({
              data1: true
            })

改为:

           that.setData({
              data1: true
            })

猜你喜欢

转载自blog.csdn.net/tianlebest/article/details/76359348