{"id":265296,"date":"2023-03-26T10:53:38","date_gmt":"2023-03-26T02:53:38","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=265296"},"modified":"2023-02-25T20:54:42","modified_gmt":"2023-02-25T12:54:42","slug":"vue-linux-watch","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/vue-linux-watch.html","title":{"rendered":"vue3\u5982\u4f55\u8fdb\u884c\u6570\u636e\u76d1\u542cwatch\/watchEffect"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u6211\u4eec\u90fd\u77e5\u9053\u76d1\u542c\u5668\u7684\u4f5c\u7528\u662f\u5728\u6bcf\u6b21\u54cd\u5e94\u5f0f\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u89e6\u53d1\uff0c\u5728\u7ec4\u5408\u5f0f API \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 watch()\u51fd\u6570\u548cwatchEffect()\u51fd\u6570,\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e0bvue3\u5982\u4f55\u8fdb\u884c\u6570\u636e\u76d1\u542cwatch\/watchEffect\uff0c\u611f\u5174\u8da3\u7684\u670b\u53cb\u4e00\u8d77\u770b\u770b\u5427<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u6211\u4eec\u90fd\u77e5\u9053\u76d1\u542c\u5668\u7684\u4f5c\u7528\u662f\u5728\u6bcf\u6b21\u54cd\u5e94\u5f0f\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u89e6\u53d1\uff0c\u5728\u7ec4\u5408\u5f0f API \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 watch()\u51fd\u6570\u548cwatchEffect()\u51fd\u6570,<\/p>\n

\u5f53\u4f60\u66f4\u6539\u4e86\u54cd\u5e94\u5f0f\u72b6\u6001\uff0c\u5b83\u53ef\u80fd\u4f1a\u540c\u65f6\u89e6\u53d1 Vue \u7ec4\u4ef6\u66f4\u65b0\u548c\u4fa6\u542c\u5668\u56de\u8c03\u3002<\/p>\n

\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u7528\u6237\u521b\u5efa\u7684\u4fa6\u542c\u5668\u56de\u8c03\uff0c\u90fd\u4f1a\u5728 Vue \u7ec4\u4ef6\u66f4\u65b0\u4e4b\u524d\u88ab\u8c03\u7528\u3002\u8fd9\u610f\u5473\u7740\u4f60\u5728\u4fa6\u542c\u5668\u56de\u8c03\u4e2d\u8bbf\u95ee\u7684 DOM \u5c06\u662f\u88ab Vue \u66f4\u65b0\u4e4b\u524d\u7684\u72b6\u6001\u3002<\/p>\n

\u90a3\u4e48\uff0c\u6211\u4eec\u6765\u770b\u4e00\u4e0b\uff0c\u600e\u4e48\u624d\u80fd\u597d\u597d\u7684\u4f7f\u7528\u4ed6\u4eec\u5462\uff1f\u4ed6\u4eec\u4e4b\u95f4\u53c8\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/p>\n

watch()\u51fd\u6570<\/strong><\/div>\n

watch \u9700\u8981\u4fa6\u542c\u7279\u5b9a\u7684\u6570\u636e\u6e90\uff0c\u6bd4\u5982\u4fa6\u542c\u4e00\u4e2a ref\uff0cwatch \u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f\u4e0d\u540c\u5f62\u5f0f\u7684\u201c\u6570\u636e\u6e90\u201d\uff1a\u5b83\u53ef\u4ee5\u662f\u4e00\u4e2a ref (\u5305\u62ec\u8ba1\u7b97\u5c5e\u6027)\u3001\u4e00\u4e2a\u54cd\u5e94\u5f0f\u5bf9\u8c61\u3001\u4e00\u4e2a getter \u51fd\u6570\u3001\u6216\u591a\u4e2a\u6570\u636e\u6e90\u7ec4\u6210\u7684\u6570\u7ec4\uff0c\u5982\u4e0b\uff1a<\/p>\n

const x = ref(0)\r\nconst y = ref(0)\r\n \r\n\/\/ \u5355\u4e2a ref\r\nwatch(x, (newX) => {\r\n  console.log(`x is ${newX}`)\r\n})\r\n \r\n\/\/ getter \u51fd\u6570\r\nwatch(\r\n  () => x.value + y.value,\r\n  (sum) => {\r\n    console.log(`sum of x + y is: ${sum}`)\r\n  }\r\n)\r\n  \r\n\/\/ \u591a\u4e2a\u6765\u6e90\u7ec4\u6210\u7684\u6570\u7ec4\r\nwatch([x, () => y.value], ([newX, newY]) => {\r\n  console.log(`x is ${newX} and y is ${newY}`)\r\n})\r\n  \r\nconst obj = reactive({ count: 0 })\r\n\/\/\u4f20\u5165\u4e00\u4e2a\u54cd\u5e94\u5f0f\u5bf9\u8c61\r\nwatch(obj, (newValue, oldValue) => {\r\n  \/\/ \u5728\u5d4c\u5957\u7684\u5c5e\u6027\u53d8\u66f4\u65f6\u89e6\u53d1\r\n  \/\/ \u6ce8\u610f\uff1a`newValue` \u6b64\u5904\u548c `oldValue` \u662f\u76f8\u7b49\u7684\r\n  \/\/ \u56e0\u4e3a\u5b83\u4eec\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff01\r\n})\r\n \r\nobj.count++\r\n  \r\nwatch(\r\n  () => obj.count,\r\n  (newValue, oldValue) => {\r\n    \/\/ \u6ce8\u610f\uff1a`newValue` \u6b64\u5904\u548c `oldValue` \u662f\u76f8\u7b49\u7684\r\n    \/\/ *\u9664\u975e* obj.count \u88ab\u6574\u4e2a\u66ff\u6362\u4e86\r\n  },\r\n  { deep: true }\r\n)<\/pre>\n

\u6ce8\u610f\uff0c\u4f60\u4e0d\u80fd\u76f4\u63a5\u4fa6\u542c\u54cd\u5e94\u5f0f\u5bf9\u8c61\u7684\u5c5e\u6027\u503c<\/p>\n

const obj = reactive({ count: 0 })\r\n \r\n\/\/ \u9519\u8bef\uff0c\u56e0\u4e3a watch() \u5f97\u5230\u7684\u53c2\u6570\u662f\u4e00\u4e2a number\r\nwatch(obj.count, (count) => {\r\n  console.log(`count is: ${count}`)\r\n})<\/pre>\n

\u8fd9\u91cc\u9700\u8981\u7528\u4e00\u4e2a\u8fd4\u56de\u8be5\u5c5e\u6027\u7684 getter \u51fd\u6570\uff1a<\/p>\n

\/\/ \u63d0\u4f9b\u4e00\u4e2a getter \u51fd\u6570\r\nwatch(\r\n  () => obj.count,\r\n  (count) => {\r\n    console.log(`count is: ${count}`)\r\n  }\r\n)<\/pre>\n

watch \u9ed8\u8ba4\u662f\u61d2\u6267\u884c\u7684\uff1a\u4ec5\u5f53\u6570\u636e\u6e90\u53d8\u5316\u65f6\uff0c\u624d\u4f1a\u6267\u884c\u56de\u8c03\u3002\u4f46\u5728\u67d0\u4e9b\u573a\u666f\u4e2d\uff0c\u6211\u4eec\u5e0c\u671b\u5728\u521b\u5efa\u4fa6\u542c\u5668\u65f6\uff0c\u7acb\u5373\u6267\u884c\u4e00\u904d\u56de\u8c03\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u6211\u4eec\u60f3\u8bf7\u6c42\u4e00\u4e9b\u521d\u59cb\u6570\u636e\uff0c\u7136\u540e\u5728\u76f8\u5173\u72b6\u6001\u66f4\u6539\u65f6\u91cd\u65b0\u8bf7\u6c42\u6570\u636e\u3002<\/p>\n

\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4f20\u5165 immediate: true \u9009\u9879\u6765\u5f3a\u5236\u4fa6\u542c\u5668\u7684\u56de\u8c03\u7acb\u5373\u6267\u884c\uff1a<\/p>\n

watch(source, (newValue, oldValue) => {\r\n  \/\/ \u7acb\u5373\u6267\u884c\uff0c\u4e14\u5f53 `source` \u6539\u53d8\u65f6\u518d\u6b21\u6267\u884c\r\n}, { immediate: true })<\/pre>\n
watchEffect()\u51fd\u6570<\/strong><\/div>\n

watchEffect() \u5141\u8bb8\u6211\u4eec\u81ea\u52a8\u8ddf\u8e2a\u56de\u8c03\u7684\u54cd\u5e94\u5f0f\u4f9d\u8d56\u3002<\/p>\n

const todoId = ref(1)\r\nconst data = ref(null)\r\n \r\nwatchEffect(async () => {\r\n  const response = await fetch(\r\n    `https:\/\/jsonplaceholder.typicode.com\/todos\/${todoId.value}`\r\n  )\r\n  data.value = await response.json()\r\n})<\/pre>\n

\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u56de\u8c03\u4f1a\u7acb\u5373\u6267\u884c\uff0c\u4e0d\u9700\u8981\u6307\u5b9a immediate: true\u3002\u5728\u6267\u884c\u671f\u95f4\uff0c\u5b83\u4f1a\u81ea\u52a8\u8ffd\u8e2a todoId.value \u4f5c\u4e3a\u4f9d\u8d56\uff08\u548c\u8ba1\u7b97\u5c5e\u6027\u7c7b\u4f3c\uff09\u3002\u6bcf\u5f53 todoId.value \u53d8\u5316\u65f6\uff0c\u56de\u8c03\u4f1a\u518d\u6b21\u6267\u884c\u3002\u6709\u4e86 watchEffect()\uff0c\u6211\u4eec\u4e0d\u518d\u9700\u8981\u660e\u786e\u4f20\u9012 todoId \u4f5c\u4e3a\u6e90\u503c\u3002<\/p>\n

watchEffect() \u9002\u5408\u6709\u591a\u4e2a\u4f9d\u8d56\u9879\u7684\u4fa6\u542c\u5668\u5bf9\u4e8e\u8fd9\u79cd\u53ea\u6709\u4e00\u4e2a\u4f9d\u8d56\u9879\u7684\u4f8b\u5b50\u6765\u8bf4\uff0c\u597d\u5904\u76f8\u5bf9\u8f83\u5c0f\u3002\u6b64\u5916\uff0c\u5982\u679c\u4f60\u9700\u8981\u4fa6\u542c\u4e00\u4e2a\u5d4c\u5957\u6570\u636e\u7ed3\u6784\u4e2d\u7684\u51e0\u4e2a\u5c5e\u6027\uff0cwatchEffect() \u53ef\u80fd\u4f1a\u6bd4\u6df1\u5ea6\u4fa6\u542c\u5668\u66f4\u6709\u6548\uff0c\u56e0\u4e3a\u5b83\u5c06\u53ea\u8ddf\u8e2a\u56de\u8c03\u4e2d\u88ab\u4f7f\u7528\u5230\u7684\u5c5e\u6027\uff0c\u800c\u4e0d\u662f\u9012\u5f52\u5730\u8ddf\u8e2a\u6240\u6709\u7684\u5c5e\u6027\u3002<\/p>\n

\u5982\u679c\u60f3\u5728\u4fa6\u542c\u5668\u56de\u8c03\u4e2d\u80fd\u8bbf\u95ee\u88ab Vue \u66f4\u65b0\u4e4b\u540e\u7684 DOM\uff0c\u4f60\u9700\u8981\u6307\u660e flush: \u2018post\u2019 \u9009\u9879\uff0c\u540e\u7f6e\u5237\u65b0\u7684 watchEffect() \u6709\u4e2a\u66f4\u65b9\u4fbf\u7684\u522b\u540d watchPostEffect()\uff1a<\/p>\n

import { watchPostEffect } from 'vue'\r\n \r\nwatchPostEffect(() => {\r\n  \/* \u5728 Vue \u66f4\u65b0\u540e\u6267\u884c *\/\r\n})<\/pre>\n
watch \u4e0e watchEffect\u4e4b\u95f4\u7684\u8054\u7cfb\u4e0e\u533a\u522b<\/strong><\/div>\n

watch \u548c watchEffect \u90fd\u80fd\u54cd\u5e94\u5f0f\u5730\u6267\u884c\u6709\u526f\u4f5c\u7528\u7684\u56de\u8c03\u3002\u5b83\u4eec\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u662f\u8ffd\u8e2a\u54cd\u5e94\u5f0f\u4f9d\u8d56\u7684\u65b9\u5f0f\uff1a<\/p>\n

watch \u53ea\u8ffd\u8e2a\u660e\u786e\u4fa6\u542c\u7684\u6570\u636e\u6e90\u3002\u5b83\u4e0d\u4f1a\u8ffd\u8e2a\u4efb\u4f55\u5728\u56de\u8c03\u4e2d\u8bbf\u95ee\u5230\u7684\u4e1c\u897f\u3002\u53e6\u5916\uff0c\u4ec5\u5728\u6570\u636e\u6e90\u786e\u5b9e\u6539\u53d8\u65f6\u624d\u4f1a\u89e6\u53d1\u56de\u8c03\u3002watch \u4f1a\u907f\u514d\u5728\u53d1\u751f\u526f\u4f5c\u7528\u65f6\u8ffd\u8e2a\u4f9d\u8d56\uff0c\u56e0\u6b64\uff0c\u6211\u4eec\u80fd\u66f4\u52a0\u7cbe\u786e\u5730\u63a7\u5236\u56de\u8c03\u51fd\u6570\u7684\u89e6\u53d1\u65f6\u673a\u3002<\/p>\n

watchEffect\uff0c\u5219\u4f1a\u5728\u526f\u4f5c\u7528\u53d1\u751f\u671f\u95f4\u8ffd\u8e2a\u4f9d\u8d56\u3002\u5b83\u4f1a\u5728\u540c\u6b65\u6267\u884c\u8fc7\u7a0b\u4e2d\uff0c\u81ea\u52a8\u8ffd\u8e2a\u6240\u6709\u80fd\u8bbf\u95ee\u5230\u7684\u54cd\u5e94\u5f0f\u5c5e\u6027\u3002\u8fd9\u66f4\u65b9\u4fbf\uff0c\u800c\u4e14\u4ee3\u7801\u5f80\u5f80\u66f4\u7b80\u6d01\uff0c\u4f46\u6709\u65f6\u5176\u54cd\u5e94\u6027\u4f9d\u8d56\u5173\u7cfb\u4f1a\u4e0d\u90a3\u4e48\u660e\u786e\u3002\u9002\u5408\u6709\u591a\u4e2a\u4f9d\u8d56\u9879\u7684\u4fa6\u542c\u5668<\/p>\n","protected":false},"excerpt":{"rendered":"

\u6211\u4eec\u90fd\u77e5\u9053\u76d1\u542c\u5668\u7684\u4f5c\u7528\u662f\u5728\u6bcf\u6b21\u54cd\u5e94\u5f0f\u72b6\u6001\u53d1\u751f\u53d8\u5316\u65f6\u89e6\u53d1\uff0c\u5728\u7ec4\u5408\u5f0f API \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 watch()\u51fd\u6570 […]<\/p>\n","protected":false},"author":362,"featured_media":83832,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-265296","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=265296"}],"version-history":[{"count":5,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265296\/revisions"}],"predecessor-version":[{"id":265301,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/265296\/revisions\/265301"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/83832"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=265296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=265296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=265296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}