首頁  >  文章  >  微信小程式  >  如何解決微信小程式回報錯誤:this.setData is not a function的問題

如何解決微信小程式回報錯誤:this.setData is not a function的問題

不言
不言原創
2018-06-27 16:15:072773瀏覽

這篇文章主要介紹了微信小程式報錯:this.setData is not a function的解決辦法的相關資料,希望透過本文能幫助到大家解決這樣類似的問題,需要的朋友可以參考下

微信小程式回報錯誤:this.setData is not a function

在page定義的程式碼如下,程式碼會報錯:this.setData is not a function

<strong> pasteEncryptedText:function()</strong>{ 
 let decryptedPass = this.data.decryptedPassword; 
 if (decryptedPass == &#39;&#39; ){ 
 wx.showToast({ 
 title: &#39;请先输入解密密码&#39;, 
 mask: true, 
 success: function (res) { 
  setTimeout(function () { 
  wx.hideToast(); 
  }, 4000); 
 }, 
 }); 
 return; 
 }else{ 
 wx.getClipboardData({ 
 <strong>success: function (res)</strong> { 
  if ( res.data == &#39;&#39; ){ 
  wx.showToast({ 
  title: &#39;剪贴板没有内容&#39;, 
  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); 
  } 
 } 
 }); 
 } 
 }

問題分析:在函數pasteEncryptedText()內嵌套呼叫另一個函數wx.showToast(),而setData ()是在wx.showToast()中呼叫的,此時this.setData() 

中的this不是page,而是wx.showToast()這個物件了 

#解決方法:

<strong> 在函数pasteEncryptedText()一开始处将this对象保存:</strong>let that = this;

#
pasteEncryptedText:function(){ 
 let decryptedPass = this.data.decryptedPassword;

##

<strong>let that = this;</strong> 
if (decryptedPass == &#39;&#39; ){ 
 wx.showToast({ 
 title: &#39;请先输入解密密码&#39;, 
 mask: true, 
 success: function (res) { 
 setTimeout(function () { 
 wx.hideToast(); 
 }, 4000); 
 }, 
 }); 
 return; 
}else{ 
 wx.getClipboardData({ 
 success: function (res) { 
 if ( res.data == &#39;&#39; ){ 
 wx.showToast({ 
  title: &#39;剪贴板没有内容&#39;, 
  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>); 
 } 
 } 
 }); 
}

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:

如何解決微信小程式請求伺服器手機預覽請求不到資料的問題

如何解決微信公眾號提示:Unauthorized API function的問題

如何解決微信小程式中出現的錯誤:{"baseresponse":{"errcode":-80002,"errmsg ":""}}

#

以上是如何解決微信小程式回報錯誤:this.setData is not a function的問題的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn