微信小程序報(bào)錯:this.setData is not a function的解決辦法
微信小程序 報(bào)錯:this.setData is not a function
在page中定義的代碼如下,代碼會報(bào)錯:this.setData is not a function
<strong> pasteEncryptedText:function()</strong>{
let decryptedPass = this.data.decryptedPassword;
if (decryptedPass == '' ){
wx.showToast({
title: '請先輸入解密密碼',
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: '剪貼板沒有內(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);
}
}
});
}
}
問題分析:在函數(shù) pasteEncryptedText()里面嵌套調(diào)用另一個函數(shù) wx.showToast(),而setData()是在wx.showToast()中調(diào)用的,此時(shí)this.setData()
中的this不是page,而是wx.showToast()這個對象了
解決方法:
<strong> 在函數(shù)pasteEncryptedText()一開始處將this對象保存:</strong>let that = this;
pasteEncryptedText:function(){
let decryptedPass = this.data.decryptedPassword;
<strong>let that = this;</strong>
if (decryptedPass == '' ){
wx.showToast({
title: '請先輸入解密密碼',
mask: true,
success: function (res) {
setTimeout(function () {
wx.hideToast();
}, 4000);
},
});
return;
}else{
wx.getClipboardData({
success: function (res) {
if ( res.data == '' ){
wx.showToast({
title: '剪貼板沒有內(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>);
}
}
});
}
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望通過本文能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
微信小程序 搖一搖抽獎簡單實(shí)例實(shí)現(xiàn)代碼
這篇文章主要介紹了微信小程序 搖一搖抽獎簡單實(shí)例實(shí)現(xiàn)代碼的相關(guān)資料,這里實(shí)現(xiàn)搖一搖抽獎的功能,需要的朋友可以參考下2017-01-01
JS實(shí)用技巧實(shí)現(xiàn)loading加載示例詳解
這篇文章主要為大家介紹了JS實(shí)用技巧實(shí)現(xiàn)loading加載示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
微信小程序 使用騰訊地圖SDK詳解及實(shí)現(xiàn)步驟
這篇文章主要介紹了微信小程序 使用騰訊地圖SDK詳解及實(shí)現(xiàn)步驟的相關(guān)資料,需要的朋友可以參考下2017-02-02
輸入框跟隨文字內(nèi)容適配寬實(shí)現(xiàn)示例
這篇文章主要為大家介紹了輸入框跟隨文字內(nèi)容適配寬實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
ChatGPT用于OA聊天助手導(dǎo)致訪問量服務(wù)宕機(jī)
這篇文章主要為大家介紹了ChatGPT用于OA聊天助手導(dǎo)致訪問量服務(wù)宕機(jī),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
用Move.js配合創(chuàng)建CSS3動畫的入門指引
這篇文章主要介紹了用Move.js配合創(chuàng)建CSS3動畫的入門指引,文中介紹了這個JavaScript庫中的一些基本方法的使用,需要的朋友可以參考下2015-07-07

