广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >微信小程序报错:this.setData is not a function怎么办
  • 817
分享到

微信小程序报错:this.setData is not a function怎么办

2024-04-02 19:04:59 817人浏览 独家记忆
摘要

这篇文章主要介绍微信小程序报错:this.setData is not a function怎么办,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!微信小程序 报错:this.setDa

这篇文章主要介绍微信小程序报错: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 == '' ){ 
 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: '剪贴板没有内容', 
  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 == '' ){ 
 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: '剪贴板没有内容', 
  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>); 
 } 
 } 
 }); 
}

以上是“微信小程序报错:this.setData is not a function怎么办”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网JavaScript频道!

--结束END--

本文标题: 微信小程序报错:this.setData is not a function怎么办

本文链接: https://www.lsjlt.com/news/70741.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作