{"id":1521,"date":"2023-03-25T14:15:00","date_gmt":"2023-03-25T06:15:00","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1521"},"modified":"2023-04-28T20:17:41","modified_gmt":"2023-04-28T12:17:41","slug":"this-points-to-the-scope-issue-in-wechat-mini-program-this-setdata-is-not-a-function-error","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/25\/this-points-to-the-scope-issue-in-wechat-mini-program-this-setdata-is-not-a-function-error\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2dthis\u6307\u5411\u4f5c\u7528\u57df\u95ee\u9898this.setData is not a function\u62a5\u9519"},"content":{"rendered":"<p>\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u6211\u4eec\u4e00\u822c\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u4fee\u6539data\u4e2d\u7684\u6570\u636e<\/p>\n<pre><code class=\"language-javascript\">this.setData({\n  data: e.detail.value\n})<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u6bd4\u5982\u5728\u51fd\u6570\u91cc\u9762\u4fee\u6539\u6570\u636e<\/p>\n<pre><code class=\"language-javascript\">bindDataChange: function (e) {\n  this.setData({\n    data: e.detail.value\n  })\n}<\/code><\/pre>\n<p>\u4f46\u662f\u5f53\u6211\u4eec\u901a\u8fc7wx.request\u8bf7\u6c42\u7f51\u7edc\u6570\u636e\u6210\u529f\u540e\u7ed1\u5b9a\u6570\u636e\u65f6\u5019\u62a5\u4ee5\u4e0b\u9519\u8bef<\/p>\n<pre><code>this.setData is not a function<\/code><\/pre>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-javascript\">requestData: function() {\n  wx.request({\n    url: url,\n    method: &#039;POST&#039;,\n    header: {\n      &#039;content-type&#039;: &#039;application\/json&#039; \/\/ \u9ed8\u8ba4\u503c\n    },\n    success: function (res) {\n      if (res.data.code == 0){\n        this.setData({\n          maxCount: res.data.maxCount\n        });\n      }\n    }\n  })\n}<\/code><\/pre>\n<p>\u8fd9\u662f\u56e0\u4e3athis\u4f5c\u7528\u57df\u6307\u5411\u95ee\u9898\uff0csuccess\u51fd\u6570\u5b9e\u9645\u662f\u4e00\u4e2a\u95ed\u5305\uff0c\u65e0\u6cd5\u76f4\u63a5\u901a\u8fc7this\u6765setData<\/p>\n<p>\u90a3\u4e48\u9700\u8981\u600e\u4e48\u4fee\u6539\u5462\uff1f\u6211\u4eec\u901a\u8fc7\u5c06\u5f53\u524d\u5bf9\u8c61\u8d4b\u7ed9\u4e00\u4e2a\u65b0\u7684\u5bf9\u8c61<\/p>\n<pre><code class=\"language-javascript\">var _this = this;<\/code><\/pre>\n<p>\u7136\u540e\u4f7f\u7528_this\u6765setData\u5373\u53ef<\/p>\n<pre><code class=\"language-javascript\">doCalc:function() {\n  var _this = this;\n  wx.request({\n    url: url,\n    method: &#039;POST&#039;,\n    header: {\n      &#039;content-type&#039;: &#039;application\/json&#039; \/\/ \u9ed8\u8ba4\u503c\n    },\n    success: function (res) {\n      if (res.data.code == 0){\n        _this.setData({\n          maxCount: res.data.maxCount\n        });\n      }\n    }\n  })\n}<\/code><\/pre>\n<p>\u53e6\u5728es6\u4e2d\uff0c\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u662f\u4e0d\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\u7684<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-javascript\">setTimeout( () =&gt; {\n  console.log(this.type + &#039; says &#039; + say)\n}, 1000)<\/code><\/pre>\n<p>\u5f53\u6211\u4eec\u4f7f\u7528\u7bad\u5934\u51fd\u6570\u65f6\uff0c\u51fd\u6570\u4f53\u5185\u7684this\u5bf9\u8c61\uff0c\u5c31\u662f\u5b9a\u4e49\u65f6\u6240\u5728\u7684\u5bf9\u8c61\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u65f6\u6240\u5728\u7684\u5bf9\u8c61\u3002\u5e76\u4e0d\u662f\u56e0\u4e3a\u7bad\u5934\u51fd\u6570\u5185\u90e8\u6709\u7ed1\u5b9athis\u7684\u673a\u5236\uff0c\u5b9e\u9645\u539f\u56e0\u662f\u7bad\u5934\u51fd\u6570\u6839\u672c\u6ca1\u6709\u81ea\u5df1\u7684this\uff0c\u5b83\u7684this\u662f\u7ee7\u627f\u5916\u9762\u7684\uff0c\u56e0\u6b64\u5185\u90e8\u7684this\u5c31\u662f\u5916\u5c42\u4ee3\u7801\u5757\u7684this\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e2d\u6211\u4eec\u4e00\u822c\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u6765\u4fee\u6539data\u4e2d\u7684\u6570\u636e this.setData({ data: e.det [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-1521","post","type-post","status-publish","format-standard","hentry","category-wechat-mini-program"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1521"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1521\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}