1、報(bào)錯(cuò)this.getUserInfo(this.setData) is not a function;at pages/index/index onShow function;at api request success callback function
TypeError: this.getUserInfo is not a function
在回調(diào)結(jié)果里調(diào)用這個(gè)頁(yè)面的函數(shù) this.fun() 或者 this.setData 時(shí)就會(huì)報(bào)錯(cuò),這時(shí)要在函數(shù)一開(kāi)時(shí)的地方使用var that = this;,然后使用that調(diào)用數(shù)據(jù)。
這里的this指向的就是請(qǐng)求本身了如( wx.request ),而不是這個(gè)頁(yè)面?。?!
onLoad:function(options){
this.login();
},
login:function(){
var that = this;// 這個(gè)地方非常重要,重置data{}里數(shù)據(jù)時(shí)候setData方法的this應(yīng)為以及函數(shù)的this, 如果在下方的sucess直接寫this就變成了wx.request()的this了
wx.login({
success: function (res) {
if (res.code) {
//發(fā)起網(wǎng)絡(luò)請(qǐng)求
wx.request({
url: ‘https://applet.ech-med.com/appwx/getAppToken‘,
data: {
code: res.code
},
success: function (re) {
console.log(re);that.getUserInfo();
that.setData({ });//如果在sucess直接寫this就變成了wx.request()的this了.必須為getdata函數(shù)的this,不然無(wú)法重置調(diào)用函數(shù)
}
})
} else {
console.log(‘獲取用戶登錄態(tài)失敗!‘ + res.errMsg)
}
}
})
},
getUserInfo: function () {
console.log("獲取用戶信息")
},