{"id":33127,"date":"2024-11-25T16:40:19","date_gmt":"2024-11-25T08:40:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/33127\/"},"modified":"2024-11-25T16:40:19","modified_gmt":"2024-11-25T08:40:19","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e9%80%9a%e8%bf%87watch%e5%92%8ccomputed%e6%a3%80%e6%b5%8b%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/33127\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u5982\u4f55\u901a\u8fc7watch\u548ccomputed\u68c0\u6d4b\u6570\u636e"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/024\/5ea6353ba1b67705.jpg\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u5982\u4f55\u901a\u8fc7watch\u548ccomputed\u68c0\u6d4b\u6570\u636e\u63d2\u56fe\" alt=\"\u5c0f\u7a0b\u5e8f\u5982\u4f55\u901a\u8fc7watch\u548ccomputed\u68c0\u6d4b\u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p>\u5728vue\u4e2d\uff0ccomputed\u662f\u4e00\u4e2a\u8ba1\u7b97\u5c5e\u6027,\u7c7b\u4f3c\u4e8e\u8fc7\u6ee4\u5668,\u5bf9\u7ed1\u5b9a\u5230view\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406,\u5e76\u76d1\u542c\u53d8\u5316\u3002\u800cwatch\u76d1\u542c\u590d\u6742\u6570\u636e\u7c7b\u578b\u9700\u7528\u6df1\u5ea6\u76d1\u542c\u3002\u8fd9\u4e24\u8005\u90fd\u53ef\u4ee5\u5728vue\u4e0a\u5b9e\u73b0\u68c0\u6d4b\u6570\u636e\u7684\u53d8\u5316\u3002\u800c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e0d\u540c\u4e8evue\u53ef\u4ee5\u4f7f\u7528watch\u548ccomputed\u505a\u51fa\u76f8\u5e94\u7684\u6539\u53d8\u3002\u5c0f\u7a0b\u5e8f\u4e2d\u53ea\u6709\u51fd\u6570this.setData()\u53ef\u4ee5\u68c0\u6d4b\u6570\u636e\uff0c\u6240\u4ee5\u5c0f\u7a0b\u5e8f\u6bcf\u6b21\u6570\u636e\u6539\u53d8\u9700\u8981\u68c0\u6d4b\u65f6\u90fd\u5fc5\u987b\u624b\u52a8\u6267\u884c\u51fd\u6570\u624d\u53ef\u5b9e\u73b0\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u5c0f\u7a0b\u5e8f\u8fd8\u53ef\u4ee5\u9644\u4e0a\u8fd9\u4e24\u4e2a\u529f\u80fd\u68c0\u6d4b\u6570\u636e\u53d8\u5316\u3002<\/p>\n<p>vue \u91cc\u662f\u901a\u8fc7&nbsp;Object.defineProperty&nbsp;\u6765\u5b9e\u73b0\u6570\u636e\u53d8\u5316\u68c0\u6d4b\u7684\uff0c\u7ed9\u8be5\u53d8\u91cf\u7684&nbsp;setter&nbsp;\u91cc\u6ce8\u5165\u6240\u6709\u7684\u7ed1\u5b9a\u64cd\u4f5c\uff0c\u5c31\u53ef\u4ee5\u5728\u8be5\u53d8\u91cf\u53d8\u5316\u65f6\u5e26\u52a8\u5176\u5b83\u6570\u636e\u7684\u53d8\u5316\u3002\u5b9e\u9645\u4e0a\uff0c\u5728\u5c0f\u7a0b\u5e8f\u91cc\u5b9e\u73b0\u8981\u6bd4&nbsp;vue&nbsp;\u91cc\u7b80\u5355\uff0c\u5e94\u4e3a\u5bf9\u4e8e&nbsp;data&nbsp;\u91cc\u5bf9\u8c61\u6765\u8bf4\uff0cvue&nbsp;\u8981\u9012\u5f52\u7684\u7ed1\u5b9a\u5bf9\u8c61\u91cc\u7684\u6bcf\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f7f\u4e4b\u54cd\u5e94\u5f0f\u5316\u3002\u4f46\u662f\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u91cc\uff0c\u4e0d\u7ba1\u662f\u5bf9\u4e8e\u5bf9\u8c61\u8fd8\u662f\u57fa\u672c\u7c7b\u578b\uff0c\u53ea\u80fd\u901a\u8fc7&nbsp;this.setData()&nbsp;\u6765\u6539\u53d8\uff0c\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u68c0\u6d4b&nbsp;data&nbsp;\u91cc\u9762\u7684&nbsp;key&nbsp;\u503c\u7684\u53d8\u5316\uff0c\u800c\u4e0d\u7528\u68c0\u6d4b&nbsp;key&nbsp;\u503c\u91cc\u9762\u7684&nbsp;key&nbsp;\u3002<\/p>\n<p>\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre>Page({\ndata:&nbsp;{\ntest:&nbsp;{&nbsp;a:&nbsp;123&nbsp;},\ntest1:&nbsp;'test1',\n},\nonLoad()&nbsp;{\ncomputed(this,&nbsp;{\ntest2:&nbsp;function()&nbsp;{\nreturnthis.data.test.a&nbsp;+&nbsp;'2222222'\n},\ntest3:&nbsp;function()&nbsp;{\nreturnthis.data.test.a&nbsp;+&nbsp;'3333333'\n}\n})\nwatch(this,&nbsp;{\ntest:function(newVal)&nbsp;{\nconsole.log('invoke&nbsp;watch')\nthis.setData({test1:&nbsp;newVal.a&nbsp;+&nbsp;'11111111'&nbsp;})\n}\n})\n},\nchangeTest()&nbsp;{\nthis.setData({&nbsp;test:{&nbsp;a:&nbsp;Math.random().toFixed(5)&nbsp;}&nbsp;})\n},\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u73b0\u5728\u6211\u4eec\u8981\u5b9e\u73b0&nbsp;watch&nbsp;\u548c&nbsp;computed&nbsp;\u65b9\u6cd5\uff0c\u4f7f\u5f97&nbsp;test&nbsp;\u53d8\u5316\u65f6\uff0ctest1\u3001test2\u3001test3&nbsp;\u4e5f\u53d8\u5316\uff0c\u4e3a\u6b64\uff0c\u589e\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\uff0c\u5f53\u70b9\u51fb\u8fd9\u4e2a\u6309\u94ae\u65f6\uff0ctest&nbsp;\u4f1a\u6539\u53d8\u3002<\/p>\n<p>watch \u65b9\u6cd5\u76f8\u5bf9\u7b80\u5355\u70b9\uff0c\u9996\u5148\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u68c0\u6d4b\u53d8\u5316\uff1a<\/p>\n<pre>function&nbsp;defineReactive(data,&nbsp;key,&nbsp;val,&nbsp;fn)&nbsp;{\nObject.defineProperty(data,&nbsp;key,&nbsp;{\nconfigurable:&nbsp;true,\nenumerable:&nbsp;true,\nget:&nbsp;function()&nbsp;{\nreturn&nbsp;val\n},\nset:&nbsp;function(newVal){\nif&nbsp;(newVal&nbsp;===&nbsp;val)return\nfn&nbsp;&amp;amp;&amp;amp;fn(newVal)\nval&nbsp;=&nbsp;newVal\n},\n})\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u540e\u904d\u5386&nbsp;watch&nbsp;\u51fd\u6570\u4f20\u5165\u7684\u5bf9\u8c61\uff0c\u7ed9\u6bcf\u4e2a\u952e\u8c03\u7528\u8be5\u65b9\u6cd5<\/p>\n<pre>function&nbsp;watch(ctx,&nbsp;obj)&nbsp;{\nObject.keys(obj).forEach(key&nbsp;=&amp;gt;&nbsp;{\ndefineReactive(ctx.data,&nbsp;key,&nbsp;ctx.data[key],&nbsp;function(value)&nbsp;{\nobj[key].call(ctx,value)\n})\n})\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u6709\u53c2\u6570\u662f&nbsp;fn&nbsp;\uff0c\u5373\u4e0a\u9762&nbsp;watch&nbsp;\u65b9\u6cd5\u91cc&nbsp;test&nbsp;\u7684\u503c\uff0c\u8fd9\u91cc\u628a\u8be5\u65b9\u6cd5\u5305\u4e00\u5c42\uff0c\u7ed1\u5b9a&nbsp;context\u3002<\/p>\n<p>\u63a5\u7740\u6765\u770b&nbsp;computed\uff0c\u8fd9\u4e2a\u7a0d\u5fae\u590d\u6742\uff0c\u56e0\u4e3a\u6211\u4eec\u65e0\u6cd5\u5f97\u77e5&nbsp;computed&nbsp;\u91cc\u4f9d\u8d56\u7684\u662f&nbsp;data&nbsp;\u91cc\u9762\u7684\u54ea\u4e2a\u53d8\u91cf\uff0c\u56e0\u6b64\u53ea\u80fd\u904d\u5386&nbsp;data&nbsp;\u91cc\u7684\u6bcf\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n<pre>function&nbsp;computed(ctx,&nbsp;obj)&nbsp;{\nlet&nbsp;keys&nbsp;=Object.keys(obj)\nlet&nbsp;dataKeys&nbsp;=Object.keys(ctx.data)\ndataKeys.forEach(dataKey&nbsp;=&amp;gt;&nbsp;{\ndefineReactive(ctx.data,&nbsp;dataKey,&nbsp;ctx.data[dataKey])\n})\nlet&nbsp;firstComputedObj&nbsp;=keys.reduce((prev,&nbsp;next)&nbsp;=&amp;gt;&nbsp;{\nctx.data.$target&nbsp;=function()&nbsp;{\nctx.setData({[next]:&nbsp;obj[next].call(ctx)&nbsp;})\n}\nprev[next]&nbsp;=obj[next].call(ctx)\nctx.data.$target&nbsp;=null&nbsp;return&nbsp;prev\n},&nbsp;{})\nctx.setData(firstComputedObj)\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8be6\u7ec6\u89e3\u91ca\u4e0b\u8fd9\u6bb5\u4ee3\u7801\uff0c\u9996\u5148\u7ed9&nbsp;data&nbsp;\u91cc\u7684\u6bcf\u4e2a\u5c5e\u6027\u8c03\u7528&nbsp;defineReactive&nbsp;\u65b9\u6cd5\u3002\u63a5\u7740\u8ba1\u7b97&nbsp;computed&nbsp;\u91cc\u9762\u6bcf\u4e2a\u5c5e\u6027\u7b2c\u4e00\u6b21\u7684\u503c\uff0c\u4e5f\u5c31\u662f\u4e0a\u4f8b\u4e2d\u7684&nbsp;test2\u3001test3\u3002<\/p>\n<pre>computed(this,&nbsp;{\ntest2:&nbsp;function()&nbsp;{\nreturnthis.data.test.a&nbsp;+&nbsp;'2222222'\n},\ntest3:&nbsp;function()&nbsp;{\nreturnthis.data.test.a&nbsp;+&nbsp;'3333333'\n}\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u91cc\u5206\u522b\u8c03\u7528&nbsp;test2&nbsp;\u548c&nbsp;test3&nbsp;\u7684\u503c\uff0c\u5c06\u8fd4\u56de\u503c\u4e0e\u5bf9\u5e94\u7684&nbsp;key&nbsp;\u503c\u7ec4\u5408\u6210\u4e00\u4e2a\u5bf9\u8c61\uff0c\u7136\u540e\u518d\u8c03\u7528&nbsp;setData()&nbsp;\uff0c\u8fd9\u6837\u5c31\u4f1a\u7b2c\u4e00\u6b21\u8ba1\u7b97\u8fd9\u4e24\u4e2a\u503c\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86&nbsp;reduce&nbsp;\u65b9\u6cd5\u3002test2&nbsp;\u548c&nbsp;test3&nbsp;\u90fd\u662f\u4f9d\u8d56&nbsp;test&nbsp;\u7684\uff0c\u8fd9\u6837\u5fc5\u987b\u5728&nbsp;test&nbsp;\u6539\u53d8\u7684\u65f6\u5019\u5728\u5176\u7684&nbsp;setter&nbsp;\u51fd\u6570\u4e2d\u8c03\u7528&nbsp;test2&nbsp;\u548c&nbsp;test3&nbsp;\u4e2d\u5bf9\u5e94\u7684\u51fd\u6570\uff0c\u5e76\u901a\u8fc7&nbsp;setData&nbsp;\u6765\u8bbe\u7f6e\u8fd9\u4e24\u4e2a\u53d8\u91cf\u3002<\/p>\n<p>\u63d0\u4f9b\u66f4\u591a\u4e0a\u7ebf\u5c0f\u7a0b\u5e8f<\/p>\n<p>\u58f0\u660e\u4e86\u4e00\u4e2a\u53d8\u91cf\u6765\u4fdd\u5b58\u6240\u6709\u5728\u53d8\u5316\u65f6\u9700\u8981\u6267\u884c\u7684\u51fd\u6570\uff0c\u5728&nbsp;set&nbsp;\u65f6\u6267\u884c\u6bcf\u4e00\u4e2a\u51fd\u6570\uff0c\u56e0\u4e3a\u6b64\u65f6&nbsp;this.data.test&nbsp;\u7684\u503c\u8fd8\u672a\u6539\u53d8\uff0c\u4f7f\u7528&nbsp;setTimeout&nbsp;\u5728\u4e0b\u4e00\u8f6e\u518d\u6267\u884c\u3002\u73b0\u5728\u5c31\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u600e\u4e48\u5c06\u51fd\u6570\u6dfb\u52a0\u5230&nbsp;subs&nbsp;\u4e2d\u3002\u4e0d\u77e5\u9053\u5404\u4f4d\u8fd8\u662f\u5426\u8bb0\u5f97\u4e0a\u9762\u6211\u4eec\u8bf4\u5230\u7684\u5728&nbsp;reduce&nbsp;\u91cc\u7684\u90a3\u4e24\u884c\u4ee3\u7801\u3002\u56e0\u4e3a\u5728\u6267\u884c\u8ba1\u7b97&nbsp;test1&nbsp;\u548c&nbsp;test2&nbsp;\u7b2c\u4e00\u6b21&nbsp;computed&nbsp;\u503c\u7684\u65f6\u5019\uff0c\u4f1a\u8c03\u7528&nbsp;test&nbsp;\u7684&nbsp;getter&nbsp;\u65b9\u6cd5\uff0c\u6b64\u523b\u5c31\u662f\u4e00\u4e2a\u597d\u673a\u4f1a\u5c06\u51fd\u6570\u6ce8\u5165\u5230&nbsp;subs&nbsp;\u4e2d\uff0c\u5728&nbsp;data&nbsp;\u4e0a\u58f0\u660e\u4e00\u4e2a&nbsp;$target&nbsp;\u53d8\u91cf\uff0c\u5e76\u5c06\u9700\u8981\u6267\u884c\u7684\u51fd\u6570\u8d4b\u503c\u7ed9\u8be5\u53d8\u91cf\uff0c\u8fd9\u6837\u5728&nbsp;getter&nbsp;\u4e2d\u5c31\u53ef\u4ee5\u5224\u65ad&nbsp;data&nbsp;\u4e0a\u6709\u65e0&nbsp;target&nbsp;\u503c\uff0c\u4ece\u800c\u5c31\u53ef\u4ee5&nbsp;push&nbsp;\u8fdb&nbsp;subs\uff0c\u8981\u6ce8\u610f\u7684\u662f\u9700\u8981\u9a6c\u4e0a\u5c06&nbsp;target&nbsp;\u8bbe\u4e3a&nbsp;null\uff0c<\/p>\n<p>\u5230\u6b64\u4e3a\u6b62\u5df2\u7ecf\u5b9e\u73b0\u4e86&nbsp;watch&nbsp;\u548c&nbsp;computed\uff0c\u4f46\u662f\u8fd8\u6ca1\u5b8c\uff0c\u6709\u4e2a\u95ee\u9898\u3002\u5f53\u540c\u65f6\u4f7f\u7528\u8fd9\u4e24\u8005\u7684\u65f6\u5019\uff0cwatch&nbsp;\u91cc\u7684\u5bf9\u8c61\u7684\u952e\u4e5f\u540c\u65f6\u5b58\u5728\u4e8e&nbsp;data&nbsp;\u4e2d\uff0c\u8fd9\u6837\u5c31\u4f1a\u91cd\u590d\u5728\u8be5\u53d8\u91cf\u4e0a\u8c03\u7528&nbsp;Object.defineProperty&nbsp;\uff0c\u540e\u9762\u4f1a\u8986\u76d6\u524d\u9762\u3002\u56e0\u4e3a\u8fd9\u91cc\u4e0d\u50cf&nbsp;vue&nbsp;\u91cc\u53ef\u4ee5\u51b3\u5b9a\u4e24\u8005\u7684\u8c03\u7528\u987a\u5e8f\uff0c\u56e0\u6b64\u6211\u4eec\u63a8\u8350\u5148\u5199&nbsp;computed&nbsp;\u518d\u5199&nbsp;watch\uff0c\u8fd9\u6837\u53ef\u4ee5&nbsp;watch computed&nbsp;\u91cc\u7684\u503c\u3002<\/p>\n<p>\u63a8\u8350\uff1a\u300a\u300b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u5982\u4f55\u901a\u8fc7watch\u548ccomputed\u68c0\u6d4b\u6570\u636e\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728vue\u4e2d\uff0ccomputed\u662f\u4e00\u4e2a\u8ba1\u7b97\u5c5e\u6027,\u7c7b\u4f3c\u4e8e\u8fc7\u6ee4\u5668,\u5bf9\u7ed1\u5b9a\u5230view\u7684\u6570\u636e\u8fdb\u884c\u5904\u7406,\u5e76\u76d1\u542c\u53d8\u5316\u3002\u800cwatch\u76d1\u542c\u590d\u6742\u6570\u636e\u7c7b\u578b\u9700\u7528\u6df1\u5ea6\u76d1\u542c\u3002\u8fd9\u4e24\u8005\u90fd\u53ef\u4ee5\u5728vue\u4e0a\u5b9e\u73b0\u68c0\u6d4b\u6570\u636e\u7684\u53d8\u5316\u3002\u800c\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e0d\u540c\u4e8evue\u53ef\u4ee5\u4f7f\u7528watch\u548ccomputed\u505a\u51fa\u76f8\u5e94\u7684\u6539\u53d8\u3002\u5c0f\u7a0b\u5e8f\u4e2d\u53ea\u6709\u51fd\u6570this.setData()\u53ef\u4ee5\u68c0\u6d4b\u6570\u636e\uff0c\u6240\u4ee5\u5c0f\u7a0b\u5e8f\u6bcf\u6b21\u6570\u636e\u6539\u53d8\u9700\u8981\u68c0\u6d4b\u65f6\u90fd\u5fc5\u987b\u624b\u52a8\u6267\u884c\u51fd\u6570\u624d\u53ef\u5b9e\u73b0\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u5c0f\u7a0b\u5e8f\u8fd8\u53ef\u4ee5\u9644\u4e0a\u8fd9\u4e24\u4e2a\u529f\u80fd\u68c0\u6d4b\u6570\u636e\u53d8\u5316\u3002 vue \u91cc\u662f\u901a\u8fc7&nbsp;Object.defineProperty&nbsp;\u6765\u5b9e\u73b0\u6570\u636e\u53d8\u5316\u68c0\u6d4b\u7684\uff0c\u7ed9\u8be5\u53d8\u91cf\u7684&nbsp;setter&nbsp;\u91cc\u6ce8\u5165\u6240\u6709\u7684\u7ed1\u5b9a\u64cd\u4f5c\uff0c\u5c31\u53ef\u4ee5\u5728\u8be5\u53d8\u91cf\u53d8\u5316\u65f6\u5e26\u52a8\u5176\u5b83\u6570\u636e\u7684\u53d8\u5316\u3002\u5b9e\u9645\u4e0a\uff0c\u5728\u5c0f\u7a0b\u5e8f\u91cc\u5b9e\u73b0\u8981\u6bd4&nbsp;vue&nbsp;\u91cc\u7b80\u5355\uff0c\u5e94\u4e3a\u5bf9\u4e8e&nbsp;data&nbsp;\u91cc\u5bf9\u8c61\u6765\u8bf4\uff0cvue&nbsp;\u8981\u9012\u5f52\u7684\u7ed1\u5b9a\u5bf9\u8c61\u91cc\u7684\u6bcf\u4e00\u4e2a\u53d8\u91cf\uff0c\u4f7f\u4e4b\u54cd\u5e94\u5f0f\u5316\u3002\u4f46\u662f\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u91cc\uff0c\u4e0d\u7ba1\u662f\u5bf9\u4e8e\u5bf9\u8c61\u8fd8\u662f\u57fa\u672c\u7c7b\u578b\uff0c\u53ea\u80fd\u901a\u8fc7&nbsp;this.setData()&nbsp;\u6765\u6539\u53d8\uff0c\u8fd9\u6837\u6211\u4eec\u53ea\u9700\u68c0\u6d4b&nbsp;data&nbsp;\u91cc\u9762\u7684&nbsp;key&nbsp;\u503c\u7684\u53d8\u5316\uff0c\u800c\u4e0d\u7528\u68c0\u6d4b&nbsp;key&nbsp;\u503c\u91cc\u9762\u7684&nbsp;key&nbsp;\u3002 \u6d4b\u8bd5\u4ee3\u7801\uff1a Page({ data:&nbsp;{ test:&nbsp;{&nbsp;a:&nbsp;123&nbsp;}, test1:&nbsp;&#8216;test1&#8217;, }, onLoad()&nbsp;{ computed(this,&nbsp;{ test2:&nbsp;function()&nbsp;{ returnthis.data.test.a&nbsp;+&nbsp;&#8216;2222222&#8217; }, test3:&nbsp;function()&nbsp;{ returnthis.data.test.a&nbsp;+&nbsp;&#8216;3333333&#8217; } }) watch(this,&nbsp;{ test:function(newVal)&nbsp;{ console.log(&#8216;invoke&nbsp;watch&#8217;) this.setData({test1:&nbsp;newVal.a&nbsp;+&nbsp;&#8216;11111111&#8217;&nbsp;}) } }) }, changeTest()&nbsp;{ this.setData({&nbsp;test:{&nbsp;a:&nbsp;Math.random().toFixed(5)&nbsp;}&nbsp;}) }, }) \u767b\u5f55\u540e\u590d\u5236 \u73b0\u5728\u6211\u4eec\u8981\u5b9e\u73b0&nbsp;watch&nbsp;\u548c&nbsp;computed&nbsp;\u65b9\u6cd5\uff0c\u4f7f\u5f97&nbsp;test&nbsp;\u53d8\u5316\u65f6\uff0ctest1\u3001test2\u3001test3&nbsp;\u4e5f\u53d8\u5316\uff0c\u4e3a\u6b64\uff0c\u589e\u52a0\u4e86\u4e00\u4e2a\u6309\u94ae\uff0c\u5f53\u70b9\u51fb\u8fd9\u4e2a\u6309\u94ae\u65f6\uff0ctest&nbsp;\u4f1a\u6539\u53d8\u3002 watch \u65b9\u6cd5\u76f8\u5bf9\u7b80\u5355\u70b9\uff0c\u9996\u5148\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u6765\u68c0\u6d4b\u53d8\u5316\uff1a function&nbsp;defineReactive(data,&nbsp;key,&nbsp;val,&nbsp;fn)&nbsp;{ Object.defineProperty(data,&nbsp;key,&nbsp;{ configurable:&nbsp;true, enumerable:&nbsp;true, get:&nbsp;function()&nbsp;{ return&nbsp;val }, set:&nbsp;function(newVal){ if&nbsp;(newVal&nbsp;===&nbsp;val)return fn&nbsp;&amp;amp;&amp;amp;fn(newVal) val&nbsp;=&nbsp;newVal }, }) } \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u904d\u5386&nbsp;watch&nbsp;\u51fd\u6570\u4f20\u5165\u7684\u5bf9\u8c61\uff0c\u7ed9\u6bcf\u4e2a\u952e\u8c03\u7528\u8be5\u65b9\u6cd5 function&nbsp;watch(ctx,&nbsp;obj)&nbsp;{ Object.keys(obj).forEach(key&nbsp;=&amp;gt;&nbsp;{ defineReactive(ctx.data,&nbsp;key,&nbsp;ctx.data[key],&nbsp;function(value)&nbsp;{ obj[key].call(ctx,value) }) }) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-33127","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=33127"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33127\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=33127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=33127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=33127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}