小程序模板網(wǎng)

信小程序報(bào)錯(cuò)記錄

發(fā)布時(shí)間:2018-02-01 16:24 所屬欄目:小程序開(kāi)發(fā)BUG
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("獲取用戶信息")
  },


易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開(kāi)源 碼云倉(cāng)庫(kù):starfork
本文地址:http://www.u-renovate.com/wxmini/doc/bug/21341.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢:800182392 點(diǎn)擊咨詢
QQ在線咨詢
AI智能客服 ×