微信小程序報(bào)錯(cuò):this.setData is not a function的解決辦法
微信小程序 報(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()一開(kāi)始處將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ì)本站的支持!
相關(guān)文章
微信小程序 Windows2008 R2服務(wù)器配置TLS1.2方法
微信小程序免費(fèi)SSL證書(shū)https、TLS版本問(wèn)題的解決方案《二十四》request:fail錯(cuò)誤(含https解決方案)(真機(jī)預(yù)覽問(wèn)題把下面的代碼復(fù)制到PowerShell里運(yùn)行一下,然后重啟服務(wù)器。# Enables TLS 1.2 on ...,需要的朋友可以參考下2016-12-12詳解微信小程序入門從這里出發(fā)(登錄注冊(cè)、開(kāi)發(fā)工具、文件及結(jié)構(gòu)介紹)
這篇文章主要介紹了微信小程序入門從這里出發(fā)(登錄注冊(cè)、開(kāi)發(fā)工具、文件及結(jié)構(gòu)介紹),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07深入講解JavaScript之繼承的多種方式和優(yōu)缺點(diǎn)
本文講主要解JavaScript各種繼承方式和優(yōu)缺點(diǎn),文章將六種繼承方式說(shuō)明,分別有原型鏈繼承、借用構(gòu)造函數(shù)(經(jīng)典繼承)、組合繼承、原型式繼承、寄生式繼承、 寄生組合式繼承,這六種方式,需要的朋友可以參考一下2021-10-10微信小程序 解析網(wǎng)頁(yè)內(nèi)容詳解及實(shí)例
這篇文章主要介紹了微信小程序 解析網(wǎng)頁(yè)內(nèi)容詳解及實(shí)例的相關(guān)資料,這里使用爬蟲(chóng)對(duì)復(fù)雜的網(wǎng)頁(yè)進(jìn)行抓取,遇到些問(wèn)題,這里整理下并解決,需要的朋友可以參考下2017-02-02一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解
這篇文章主要為大家介紹了一款功能強(qiáng)大的markdown編輯器tui.editor使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Meta開(kāi)源JavaScript內(nèi)存泄漏監(jiān)測(cè)工具M(jìn)emLab安裝使用
這篇文章主要為大家介紹了Meta開(kāi)源JavaScript內(nèi)存泄漏監(jiān)測(cè)工具M(jìn)emLab安裝使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09