{"id":52958,"date":"2024-12-03T08:36:29","date_gmt":"2024-12-03T00:36:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52958\/"},"modified":"2024-12-03T08:36:29","modified_gmt":"2024-12-03T00:36:29","slug":"vue3-computed-%e4%b8%ad%e7%9a%84%e5%be%aa%e7%8e%af%e6%89%a7%e8%a1%8c%e5%a6%82%e4%bd%95%e9%81%bf%e5%85%8d%e6%a0%88%e6%ba%a2%e5%87%ba%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52958\/","title":{"rendered":"Vue3 computed \u4e2d\u7684\u5faa\u73af\u6267\u884c\u5982\u4f55\u907f\u514d\u6808\u6ea2\u51fa\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Vue3 computed \u4e2d\u7684\u5faa\u73af\u6267\u884c\u5982\u4f55\u907f\u514d\u6808\u6ea2\u51fa\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aVue3 computed \u4e2d\u7684\u5faa\u73af\u6267\u884c\u5982\u4f55\u907f\u514d\u6808\u6ea2\u51fa\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/17325007636743dd1b1a8f3.jpg\" class=\"aligncenter\" title=\"Vue3 computed \u4e2d\u7684\u5faa\u73af\u6267\u884c\u5982\u4f55\u907f\u514d\u6808\u6ea2\u51fa\uff1f\u63d2\u56fe\" alt=\"Vue3 computed \u4e2d\u7684\u5faa\u73af\u6267\u884c\u5982\u4f55\u907f\u514d\u6808\u6ea2\u51fa\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>vue3 computed \u4e2d\u7684\u5faa\u73af\u6267\u884c\u5bfc\u81f4\u6808\u6ea2\u51fa<\/strong><\/p>\n<p>\u5728 vue3 \u4e2d\u4f7f\u7528 computed \u5c5e\u6027\u65f6\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u662f\u4ee3\u7801\u4e2d\u7684\u5faa\u73af\u6267\u884c\uff0c\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002\u8ba9\u6211\u4eec\u5206\u6790\u4e00\u4e0b\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\u548c\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p>\u5728\u4f60\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff0cmindate\u548cmaxdate\u4e24\u4e2a computed \u5c5e\u6027\u90fd\u4f9d\u8d56\u4e8e props.checkdate \u6570\u7ec4\u3002\u5f53 checkdate \u6570\u7ec4\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u8fd9\u4e24\u4e2a\u5c5e\u6027\u90fd\u4f1a\u91cd\u65b0\u8ba1\u7b97\u3002\u7136\u800c\uff0c\u5728\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\uff0c\u8fd9\u4e9b\u5c5e\u6027\u4f1a\u5bf9 checkdate \u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5b83\u4eec\u76f8\u4e92\u89e6\u53d1\u91cd\u65b0\u8ba1\u7b97\uff0c\u4ece\u800c\u5f62\u6210\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528 ref \u6765\u5b58\u50a8\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u3002<\/li>\n<li>\u5728 checkdate \u53d1\u751f\u53d8\u5316\u65f6\uff0c\u66f4\u65b0\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u3002<\/li>\n<li>\u5728 computed \u5c5e\u6027\u4e2d\u4f7f\u7528\u6392\u5e8f\u540e\u7684\u6570\u7ec4\uff0c\u800c\u4e0d\u662f\u5bf9 checkdate \u8fdb\u884c\u6392\u5e8f\u3002<\/li>\n<\/ul>\n<p>\u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>&lt;script lang=\"ts\" setup&gt;\nimport { ref, computed, watch, onMounted } from 'vue';\n\nconst props = defineProps({\n  checkDate: {\n    type: Array,\n    default() {\n      return [];\n    }\n  }\n});\n\n\/\/ \u65b0\u5efa\u4e00\u4e2a\u54cd\u5e94\u5f0f\u5c5e\u6027\u6765\u5b58\u50a8\u6392\u5e8f\u540e\u7684\u6570\u7ec4\nconst sortedCheckDates = ref([]);\n\nconst minDate = computed(() =&gt; {\n  if (sortedCheckDates.value.length) {\n    return new Date(sortedCheckDates.value[0].getTime());\n  } else {\n    return new Date();\n  }\n});\n\nconst maxDate = computed(() =&gt; {\n  if (sortedCheckDates.value.length) {\n    return new Date(sortedCheckDates.value[sortedCheckDates.value.length - 1].getTime());\n  } else {\n    return new Date();\n  }\n});\n\n\/\/ \u76d1\u542ccheckDate\u7684\u53d8\u5316\u5e76\u66f4\u65b0sortedCheckDates\nwatch(() =&gt; props.checkDate, (newVal) =&gt; {\n  sortedCheckDates.value = newVal.sort((a, b) =&gt; a.getTime() - b.getTime());\n});\n\nconst curYear = ref(new Date().getFullYear());\nconst curMonth = ref(new Date().getMonth());\n\nwatch(() =&gt; maxDate.value, (newVal) =&gt; {\n  if (newVal) {\n    curYear.value = newVal.getFullYear();\n    curMonth.value = newVal.getMonth();\n  }\n}, {\n  immediate: true\n});\n\nonMounted(() =&gt; {\n  \/\/ \u521d\u59cb\u5316sortedCheckDates\n  sortedCheckDates.value = props.checkDate.sort((a, b) =&gt; a.getTime() - b.getTime());\n});\n&lt;\/script&gt;<\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u89e3\u51b3\u4e86 computed \u5c5e\u6027\u4e2d\u7684\u65e0\u9650\u5faa\u73af\uff0c\u907f\u514d\u4e86\u6808\u6ea2\u51fa\u9519\u8bef\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   win11\u5b9a\u65f6\u5173\u673a\u8bbe\u7f6e\u5728\u54ea win11\u5b9a\u65f6\u5173\u673a\u5728\u54ea\u91cc\u8bbe\u7f6e\u7535\u8111\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Vue3 computed \u4e2d\u7684\u5faa\u73af\u6267\u884c\u5982\u4f55\u907f\u514d\u6808\u6ea2\u51fa\uff1f \u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60\u6587\u7ae0\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765\u300aVue3 computed \u4e2d\u7684\u5faa\u73af\u6267\u884c\u5982\u4f55\u907f\u514d\u6808\u6ea2\u51fa\uff1f\u300b\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01 vue3 computed \u4e2d\u7684\u5faa\u73af\u6267\u884c\u5bfc\u81f4\u6808\u6ea2\u51fa \u5728 vue3 \u4e2d\u4f7f\u7528 computed \u5c5e\u6027\u65f6\uff0c\u4e00\u4e2a\u5e38\u89c1\u7684\u9519\u8bef\u662f\u4ee3\u7801\u4e2d\u7684\u5faa\u73af\u6267\u884c\uff0c\u5bfc\u81f4\u6808\u6ea2\u51fa\u3002\u8ba9\u6211\u4eec\u5206\u6790\u4e00\u4e0b\u8fd9\u4e2a\u95ee\u9898\u7684\u539f\u56e0\u548c\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6848\u3002 \u5728\u4f60\u63d0\u4f9b\u7684\u4ee3\u7801\u4e2d\uff0cmindate\u548cmaxdate\u4e24\u4e2a computed \u5c5e\u6027\u90fd\u4f9d\u8d56\u4e8e props.checkdate \u6570\u7ec4\u3002\u5f53 checkdate \u6570\u7ec4\u53d1\u751f\u53d8\u5316\u65f6\uff0c\u8fd9\u4e24\u4e2a\u5c5e\u6027\u90fd\u4f1a\u91cd\u65b0\u8ba1\u7b97\u3002\u7136\u800c\uff0c\u5728\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\uff0c\u8fd9\u4e9b\u5c5e\u6027\u4f1a\u5bf9 checkdate \u6570\u7ec4\u8fdb\u884c\u6392\u5e8f\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u5b83\u4eec\u76f8\u4e92\u89e6\u53d1\u91cd\u65b0\u8ba1\u7b97\uff0c\u4ece\u800c\u5f62\u6210\u65e0\u9650\u5faa\u73af\u3002 \u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u53ef\u4ee5\u91c7\u53d6\u4ee5\u4e0b\u6b65\u9aa4\uff1a \u4f7f\u7528 ref \u6765\u5b58\u50a8\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u3002 \u5728 checkdate \u53d1\u751f\u53d8\u5316\u65f6\uff0c\u66f4\u65b0\u6392\u5e8f\u540e\u7684\u6570\u7ec4\u3002 \u5728 computed \u5c5e\u6027\u4e2d\u4f7f\u7528\u6392\u5e8f\u540e\u7684\u6570\u7ec4\uff0c\u800c\u4e0d\u662f\u5bf9 checkdate \u8fdb\u884c\u6392\u5e8f\u3002 \u4fee\u6539\u540e\u7684\u4ee3\u7801\u5982\u4e0b\uff1a &lt;script lang=&#8221;ts&#8221; setup&gt; import { ref, computed, watch, onMounted } from &#8216;vue&#8217;; const props = defineProps({ checkDate: { type: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-52958","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52958","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=52958"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52958\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52958"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52958"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52958"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}