微信小程序 報(bào)錯(cuò):this.setData is not a function
在page中定義的代碼如下,代碼會(huì)報(bào)錯(cuò):this.setData is not a function
<strong> pasteEncryptedText:function()</strong>{ let decryptedPass = this.data.decryptedPassword; if (decryptedPass == '' ){ wx.showToast({ title: '請(qǐng)先輸入解密密碼', mask: true, success: function (res) { setTimeout(function () { wx.hideToast(); }, 4000); }, }); return; }else{ wx.getClipboardData({ <strong>success: function (res)</strong> { if ( res.data == '' ){ wx.showToast({ title: '剪貼板沒(méi)有內(nèi)容', mask: true, success: function (res) { setTimeout(function () { wx.hideToast(); }, 4000); }, }) }else{ console.log(decryptedPass); console.log(res.data); <strong>this.setData({ encryptedTextDecode: res.data, originalTextDecode: desEncryptedDecrypted.decrypt(res.data, decryptedPass), });</strong> console.log(this.data.originalTextDecode); } } }); } }
問(wèn)題分析:在函數(shù) pasteEncryptedText()里面嵌套調(diào)用另一個(gè)函數(shù) wx.showToast(),而setData()是在wx.showToast()中調(diào)用的,此時(shí)this.setData()
中的this不是page,而是wx.showToast()這個(gè)對(duì)象了
解決方法:
<strong> 在函數(shù)pasteEncryptedText()一開始處將this對(duì)象保存:</strong>let that = this;
pasteEncryptedText:function(){ let decryptedPass = this.data.decryptedPassword;
<strong>let that = this;</strong> if (decryptedPass == '' ){ wx.showToast({ title: '請(qǐng)先輸入解密密碼', mask: true, success: function (res) { setTimeout(function () { wx.hideToast(); }, 4000); }, }); return; }else{ wx.getClipboardData({ success: function (res) { if ( res.data == '' ){ wx.showToast({ title: '剪貼板沒(méi)有內(nèi)容', mask: true, success: function (res) { setTimeout(function () { wx.hideToast(); }, 4000); }, }) }else{ console.log(decryptedPass); console.log(res.data); <strong> that.setData</strong>({ encryptedTextDecode: res.data, originalTextDecode: desEncryptedDecrypted.decrypt(res.data, decryptedPass), }); console.log(<strong>that.data.originalTextDecode</strong>); } } }); }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望通過(guò)本文能幫助到大家,謝謝大家對(duì)本站的支持!
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com