Cannot read property 'setData' of undefined

在用button的open-type获取用户信息时遇到了这个错误。

wxml


回调函数bindgetuserinfo:

bindgetuserinfo: (e) => {
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true,
    })
  }

原因在于:箭头函数没有自己的this指针,如果有,那是外层代码块的this

将回调函数改成这样就没问题了

bindgetuserinfo: funtion (e) {
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true,
    })
  }

你可能感兴趣的:(miniProgram)