{"id":52550,"date":"2024-12-03T17:45:49","date_gmt":"2024-12-03T09:45:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/52550\/"},"modified":"2024-12-03T17:45:49","modified_gmt":"2024-12-03T09:45:49","slug":"vue3-computed-%e5%b1%9e%e6%80%a7%e7%9b%b8%e4%ba%92%e4%be%9d%e8%b5%96%e5%af%bc%e8%87%b4%e6%a0%88%e6%ba%a2%e5%87%ba%ef%bc%9f%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/52550\/","title":{"rendered":"Vue3 computed \u5c5e\u6027\u76f8\u4e92\u4f9d\u8d56\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1f\u5982\u4f55\u89e3\u51b3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>Vue3 computed \u5c5e\u6027\u76f8\u4e92\u4f9d\u8d56\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1f\u5982\u4f55\u89e3\u51b3\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aVue3 computed \u5c5e\u6027\u76f8\u4e92\u4f9d\u8d56\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1f\u5982\u4f55\u89e3\u51b3\uff1f\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241121\/1732174605673ee30db3883.jpg\" class=\"aligncenter\" title=\"Vue3 computed \u5c5e\u6027\u76f8\u4e92\u4f9d\u8d56\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1f\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" alt=\"Vue3 computed \u5c5e\u6027\u76f8\u4e92\u4f9d\u8d56\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1f\u5982\u4f55\u89e3\u51b3\uff1f\u63d2\u56fe\" \/><\/p>\n<h2>vue3 computed \u4e2d\u7684\u4ee3\u7801\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1f<\/h2>\n<h3>\u95ee\u9898\uff1a<\/h3>\n<p>\u4e00\u6bb5 vue3 \u4ee3\u7801\u4e2d\u4f7f\u7528 computed \u5bfc\u81f4\u4e86\u6808\u6ea2\u51fa\uff0c\u539f\u56e0\u4e0d\u660e\u786e\u3002<\/p>\n<pre>\/\/ index.vue\n&lt;custom-calendar :check-date=\"checkdate\" class=\"calendar-box\" \/&gt;\n\n\/\/ customcalendar.vaue\n&lt;script lang=\"ts\" setup&gt;\nconst props = defineprops({\n  checkdate: {\n    type: array,\n    default() {\n      return [];\n    },\n  },\n});\n\nconst mindate = computed(() =&gt; {\n  if (props.checkdate.length) {\n    let newarr = props.checkdate.sort((a, b): number =&gt; a.gettime() - b.gettime());\n    return new date(+newarr[0] as number);\n  } else {\n    return new date();\n  }\n});\n\nconst maxdate = computed(() =&gt; {\n  if (props.checkdate.length) {\n    let newarr = props.checkdate.sort((a, b): number =&gt; b.gettime() - a.gettime());\n    return new date(+newarr[0] as number);\n  } else {\n    return new date();\n  }\n});\n\nconst curyear = ref&lt;number&gt;(new date().getfullyear());\nconst curmonth = ref&lt;number&gt;(new date().getmonth());\n\nwatch(() =&gt; maxdate.value, (newval: date | null) =&gt; {\n  if (newval) {\n    curyear.value = newval.getfullyear();\n    curmonth.value = newval.getmonth();\n  }\n}, {\n  immediate: true,\n});\n&lt;\/script&gt;<\/pre>\n<h3>\u7b54\u6848\uff1a<\/h3>\n<p>\u8be5\u95ee\u9898\u662f\u7531\u76f8\u4e92\u4f9d\u8d56\u7684 computed \u5c5e\u6027\u5bfc\u81f4\u7684\u65e0\u9650\u5faa\u73af\u5f15\u8d77\u7684\u3002<\/p>\n<p>mindate \u548c maxdate computed \u5c5e\u6027\u90fd\u4f9d\u8d56\u4e8e props.checkdate \u6570\u7ec4\u3002\u5982\u679c checkdate \u6570\u7ec4\u53d1\u751f\u53d8\u5316\uff0c\u8fd9\u4e24\u4e2a\u5c5e\u6027\u90fd\u4f1a\u91cd\u65b0\u8ba1\u7b97\u3002\u5728\u91cd\u65b0\u8ba1\u7b97\u8fc7\u7a0b\u4e2d\uff0c\u5b83\u4eec\u53c8\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\u5f62\u6210\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5bf9\u4ee3\u7801\u7684\u4fee\u6539\uff0c\u4ee5\u89e3\u51b3\u6b64\u95ee\u9898\uff1a<\/p>\n<pre>import { 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\u589e\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\uff0c\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});<\/pre>\n<p>\u6b64\u4fee\u6539\u901a\u8fc7\u5f15\u5165\u4e00\u4e2a\u989d\u5916\u7684\u54cd\u5e94\u5f0f\u5c5e\u6027 sortedcheckdates \u6765\u5b58\u50a8\u6392\u5e8f\u540e\u7684 checkdate \u6570\u7ec4\uff0c\u89e3\u51b3\u4e86 computed \u5c5e\u6027\u4e4b\u95f4\u7684\u76f8\u4e92\u4f9d\u8d56\u6027\u3002\u73b0\u5728\uff0c\u5f53 checkdate \u6570\u7ec4\u53d1\u751f\u53d8\u5316\u65f6\uff0csortedcheckdates \u6570\u7ec4\u5c06\u66f4\u65b0\uff0c\u5e76\u7531 computed \u5c5e\u6027\u4f7f\u7528\u5b83\uff0c\u4ece\u800c\u907f\u514d\u4e86\u65e0\u9650\u5faa\u73af\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt><\/dt>\n<dd>\n   \u5982\u4f55\u89e3\u51b3 GO \u8bed\u8a00\u4e2d\u7ed3\u6784\u4f53 Map \u5b57\u6bb5\u7684\u81ea\u52a8\u521d\u59cb\u5316\u95ee\u9898\uff1f\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Vue3 computed \u5c5e\u6027\u76f8\u4e92\u4f9d\u8d56\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1f\u5982\u4f55\u89e3\u51b3\uff1f \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300aVue3 computed \u5c5e\u6027\u76f8\u4e92\u4f9d\u8d56\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1f\u5982\u4f55\u89e3\u51b3\uff1f\u300b\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a\u6587\u7ae0\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~ vue3 computed \u4e2d\u7684\u4ee3\u7801\u5bfc\u81f4\u6808\u6ea2\u51fa\uff1f \u95ee\u9898\uff1a \u4e00\u6bb5 vue3 \u4ee3\u7801\u4e2d\u4f7f\u7528 computed \u5bfc\u81f4\u4e86\u6808\u6ea2\u51fa\uff0c\u539f\u56e0\u4e0d\u660e\u786e\u3002 \/\/ index.vue &lt;custom-calendar :check-date=&#8221;checkdate&#8221; class=&#8221;calendar-box&#8221; \/&gt; \/\/ customcalendar.vaue &lt;script lang=&#8221;ts&#8221; setup&gt; const props = defineprops({ checkdate: { type: array, default() { return []; }, }, }); const mindate = computed(() =&gt; { if (props.checkdate.length) { let newarr = props.checkdate.sort((a, b): [&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-52550","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52550","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=52550"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/52550\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=52550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=52550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=52550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}