异常处理——this.setData is not a function报错处理

微信代码:

 wx.getUserInfo({
      success: function (res) {      
        this.setData({
          sex: res.userInfo.gender
        })     
      }
    })

微信报错

this.setData is not a function;at pages/personal/index/index onLoad function;

解决方法  加上var that=this  原因

1、如果函数作为对象的方法调用,this指向的是这个上级对象,即调用方法的对象。
2、如果是构造函数中的this,则this指向新创建的对象本身。

 onLoad() {
    var that = this

    wx.getUserInfo({
      success: function (res) {      
        that.setData({
          sex: res.userInfo.gender
        })     
      }
    })
}

猜你喜欢

转载自blog.csdn.net/Dj_Fairy/article/details/81186005