wx.chooseImage({ count: 3, sizeType: ['original'], sourceType: ['album', 'camera'], success (res) { // tempFilePath可以作為img標簽的src屬性顯示圖片 const tempFilePaths = res.tempFilePaths; this.setData({ imgPaths:tempFilePaths }); }, fail(err){ } }); }, |
VM6263:1 thirdScriptError Cannot read property 'setData' of undefined;at api chooseImage success callback function TypeError: Cannot read property 'setData' of undefined at success (http://127.0.0.1:43580/appservice/pages/comment/comment.js:42:14) at Function.o.<computed> (WAService.js:1:1116874) at Object.success (WAService.js:1:102889) at r (WAService.js:1:418891) at WAService.js:1:419068 at v (WAService.js:1:419077) at WAService.js:1:420485 at t.<anonymous> (http://127.0.0.1:43580/appservice/__dev__/asdebug.js:1:10431) at WAService.js:1:102889 at WAService.js:1:90451 |
錯誤原因
普通函數(shù)中和ES6箭頭函數(shù)中this的區(qū)別
舉例
//上傳圖片 uploadImg:function(event){ //1.選擇圖片 var _this=this; //如果想要在下面的success回調(diào)函數(shù)中使用全局this對象,這里需要進行變量轉(zhuǎn)換。 wx.chooseImage({ count: 3, sizeType: ['original'], sourceType: ['album', 'camera'], success (res) { const tempFilePaths = res.tempFilePaths; _this.setData({ imgPaths:tempFilePaths }); }, fail(err){ } }); }, |
//上傳圖片 uploadImg:function(event){ //1.選擇圖片 // var _this=this; wx.chooseImage({ count: 3, sizeType: ['original'], sourceType: ['album', 'camera'], success :res=> { //如果使用箭頭函數(shù),回調(diào)函數(shù)內(nèi)就可以直接使用this對象,因為this已經(jīng)繼承了uploadImg的全局this對象 const tempFilePaths = res.tempFilePaths; this.setData({ imgPaths:tempFilePaths }); }, fail:err=>{ } }); }, |
工作日 8:30-12:00 14:30-18:00
周六及部分節(jié)假日提供值班服務