微信小程序中 ,API請求數據顯示 this.setData is not a function
原因:this指向問題
解決:在函數中加入var that = this;使用that.setData()代碼如下
1 onLoad: function (options) { 2 var that = this; 3 // 發送異步請求,獲取輪播圖數據 4 wx.request({ 5 url: 'http://localhost:3000/getlunbotu', // 請求路徑 6 data: '', // 發送數據給后台 7 header: {}, // 請求頭 8 method: 'GET', // 請求方式 9 dataType: 'json', // 返回值類型,默認json 10 responseType: 'text', // 文本類型 11 success: function (res) { // 成功后 12 // console.log(res.data) 13 that.setData({ 14 swiperList: res.data 15 }) 16 }, 17 fail: function(res) {}, // 失敗后 18 complete: function(res) {}, // 成功或失敗后都會執行 19 }) 20 },
1、如果函數作為對象的方法調用,this指向的是這個上級對象,即調用方法的對象。
2、如果是構造函數中的this,則this指向新創建的對象本身。