{"id":185796,"date":"2023-08-12T08:42:28","date_gmt":"2023-08-12T00:42:28","guid":{"rendered":"http:\/\/www.idc.net\/help\/185796\/"},"modified":"2023-08-12T08:42:28","modified_gmt":"2023-08-12T00:42:28","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%95%99%e7%a8%8b%ef%bc%9a%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f-%e6%95%b0%e6%8d%ae%e7%9b%91%e5%90%ac%e5%99%a8","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/185796\/","title":{"rendered":"\u540e\u6d6a\u4e91\u5c0f\u7a0b\u5e8f\u6559\u7a0b\uff1a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f \u6570\u636e\u76d1\u542c\u5668"},"content":{"rendered":"<h2>\u6570\u636e\u76d1\u542c\u5668<\/h2>\n<p>\u6570\u636e\u76d1\u542c\u5668\u53ef\u4ee5\u7528\u4e8e\u76d1\u542c\u548c\u54cd\u5e94\u4efb\u4f55\u5c5e\u6027\u548c\u6570\u636e\u5b57\u6bb5\u7684\u53d8\u5316\u3002\u4ece\u5c0f\u7a0b\u5e8f\u57fa\u7840\u5e93\u7248\u672c&nbsp;2.6.1&nbsp;\u5f00\u59cb\u652f\u6301\u3002<\/p>\n<h2>\u4f7f\u7528\u6570\u636e\u76d1\u542c\u5668<\/h2>\n<p>\u6709\u65f6\uff0c\u5728\u4e00\u4e9b\u6570\u636e\u5b57\u6bb5\u88ab setData \u8bbe\u7f6e\u65f6\uff0c\u9700\u8981\u6267\u884c\u4e00\u4e9b\u64cd\u4f5c\u3002<\/p>\n<p>\u4f8b\u5982\uff0c&nbsp;this.data.sum&nbsp;\u6c38\u8fdc\u662f&nbsp;this.data.numberA&nbsp;\u4e0e&nbsp;this.data.numberB&nbsp;\u7684\u548c\u3002\u6b64\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u6570\u636e\u76d1\u542c\u5668\u8fdb\u884c\u5982\u4e0b\u5b9e\u73b0\u3002<\/p>\n<pre><code>Component({\n  attached: function() {\n    this.setData({\n      numberA: 1,\n      numberB: 2,\n    })\n  },\n  observers: {\n    'numberA, numberB': function(numberA, numberB) {\n      \/\/ \u5728 numberA \u6216\u8005 numberB \u88ab\u8bbe\u7f6e\u65f6\uff0c\u6267\u884c\u8fd9\u4e2a\u51fd\u6570\n      this.setData({\n        sum: numberA + numberB\n      })\n    }\n  }\n})<\/code><\/pre>\n<h2>\u76d1\u542c\u5b57\u6bb5\u8bed\u6cd5<\/h2>\n<p>\u6570\u636e\u76d1\u542c\u5668\u652f\u6301\u76d1\u542c\u5c5e\u6027\u6216\u5185\u90e8\u6570\u636e\u7684\u53d8\u5316\uff0c\u53ef\u4ee5\u540c\u65f6\u76d1\u542c\u591a\u4e2a\u3002\u4e00\u6b21 setData \u6700\u591a\u89e6\u53d1\u6bcf\u4e2a\u76d1\u542c\u5668\u4e00\u6b21\u3002<\/p>\n<p>\u540c\u65f6\uff0c\u76d1\u542c\u5668\u53ef\u4ee5\u76d1\u542c\u5b50\u6570\u636e\u5b57\u6bb5\uff0c\u5982\u4e0b\u4f8b\u6240\u793a\u3002<\/p>\n<pre><code>Component({\n  observers: {\n    'some.subfield': function(subfield) {\n      \/\/ \u4f7f\u7528 setData \u8bbe\u7f6e this.data.some.subfield \u65f6\u89e6\u53d1\n      \/\/ \uff08\u9664\u6b64\u4ee5\u5916\uff0c\u4f7f\u7528 setData \u8bbe\u7f6e this.data.some \u4e5f\u4f1a\u89e6\u53d1\uff09\n      subfield === this.data.some.subfield\n    },\n    'arr[12]': function(arr12) {\n      \/\/ \u4f7f\u7528 setData \u8bbe\u7f6e this.data.arr[12] \u65f6\u89e6\u53d1\n      \/\/ \uff08\u9664\u6b64\u4ee5\u5916\uff0c\u4f7f\u7528 setData \u8bbe\u7f6e this.data.arr \u4e5f\u4f1a\u89e6\u53d1\uff09\n      arr12 === this.data.arr[12]\n    },\n  }\n})\n<\/code><\/pre>\n<p>\u5982\u679c\u9700\u8981\u76d1\u542c\u6240\u6709\u5b50\u6570\u636e\u5b57\u6bb5\u7684\u53d8\u5316\uff0c\u53ef\u4ee5\u4f7f\u7528\u901a\u914d\u7b26&nbsp;**&nbsp;\u3002<\/p>\n<pre><code>Component({\n  observers: {\n    'some.field.**': function(field) {\n      \/\/ \u4f7f\u7528 setData \u8bbe\u7f6e this.data.some.field \u672c\u8eab\u6216\u5176\u4e0b\u4efb\u4f55\u5b50\u6570\u636e\u5b57\u6bb5\u65f6\u89e6\u53d1\n      \/\/ \uff08\u9664\u6b64\u4ee5\u5916\uff0c\u4f7f\u7528 setData \u8bbe\u7f6e this.data.some \u4e5f\u4f1a\u89e6\u53d1\uff09\n      field === this.data.some.field\n    },\n  },\n  attached: function() {\n    \/\/ \u8fd9\u6837\u4f1a\u89e6\u53d1\u4e0a\u9762\u7684 observer\n    this.setData({\n      'some.field': { \/* ... *\/ }\n    })\n    \/\/ \u8fd9\u6837\u4e5f\u4f1a\u89e6\u53d1\u4e0a\u9762\u7684 observer\n    this.setData({\n      'some.field.xxx': { \/* ... *\/ }\n    })\n    \/\/ \u8fd9\u6837\u8fd8\u662f\u4f1a\u89e6\u53d1\u4e0a\u9762\u7684 observer\n    this.setData({\n      'some': { \/* ... *\/ }\n    })\n  }\n})\n<\/code><\/pre>\n<p>\u7279\u522b\u5730\uff0c\u4ec5\u4f7f\u7528\u901a\u914d\u7b26&nbsp;**&nbsp;\u53ef\u4ee5\u76d1\u542c\u5168\u90e8 setData \u3002<\/p>\n<pre><code>Component({\n  observers: {\n    '**': function() {\n      \/\/ \u6bcf\u6b21 setData \u90fd\u89e6\u53d1\n    },\n  },\n})\n<\/code><\/pre>\n<p><b>\u63d0\u793a\uff1a<\/b><\/p>\n<ul>\n<li>\u6570\u636e\u76d1\u542c\u5668\u76d1\u542c\u7684\u662f setData \u6d89\u53ca\u5230\u7684\u6570\u636e\u5b57\u6bb5\uff0c\u5373\u4f7f\u8fd9\u4e9b\u6570\u636e\u5b57\u6bb5\u7684\u503c\u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff0c\u6570\u636e\u76d1\u542c\u5668\u4f9d\u7136\u4f1a\u88ab\u89e6\u53d1\u3002<\/li>\n<li>\u5982\u679c\u5728\u6570\u636e\u76d1\u542c\u5668\u51fd\u6570\u4e2d\u4f7f\u7528 setData \u8bbe\u7f6e\u672c\u8eab\u76d1\u542c\u7684\u6570\u636e\u5b57\u6bb5\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6b7b\u5faa\u73af\uff0c\u9700\u8981\u7279\u522b\u7559\u610f\u3002<\/li>\n<li>\u6570\u636e\u76d1\u542c\u5668\u548c\u5c5e\u6027\u7684 observer \u76f8\u6bd4\uff0c\u6570\u636e\u76d1\u542c\u5668\u66f4\u5f3a\u5927\u4e14\u901a\u5e38\u5177\u6709\u66f4\u597d\u7684\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6570\u636e\u76d1\u542c\u5668 \u6570\u636e\u76d1\u542c\u5668\u53ef\u4ee5\u7528\u4e8e\u76d1\u542c\u548c\u54cd\u5e94\u4efb\u4f55\u5c5e\u6027\u548c\u6570\u636e\u5b57\u6bb5\u7684\u53d8\u5316\u3002\u4ece\u5c0f\u7a0b\u5e8f\u57fa\u7840\u5e93\u7248\u672c&nbsp;2.6.1&#038;n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182398],"tags":[],"class_list":["post-185796","post","type-post","status-publish","format-standard","hentry","category-mini-program"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185796","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=185796"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/185796\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=185796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=185796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=185796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}