天天看點

微信小程式 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
 })      

來源:http://www.cnblogs.com/cocozj945/p/7068719.html