{"id":524,"date":"2025-12-05T19:34:45","date_gmt":"2025-12-05T11:34:45","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=524"},"modified":"2025-12-05T19:34:45","modified_gmt":"2025-12-05T11:34:45","slug":"vue3%e5%89%8d%e7%ab%af%e9%a1%b9%e7%9b%ae%e5%ae%9e%e7%8e%b0%e5%8a%a8%e6%80%81%e6%98%be%e7%a4%ba%e5%bd%93%e5%89%8d%e7%b3%bb%e7%bb%9f%e6%97%b6%e9%97%b4%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/vue3%e5%89%8d%e7%ab%af%e9%a1%b9%e7%9b%ae%e5%ae%9e%e7%8e%b0%e5%8a%a8%e6%80%81%e6%98%be%e7%a4%ba%e5%bd%93%e5%89%8d%e7%b3%bb%e7%bb%9f%e6%97%b6%e9%97%b4%e8%af%a6%e8%a7%a3\/","title":{"rendered":"Vue3\u524d\u7aef\u9879\u76ee\u5b9e\u73b0\u52a8\u6001\u663e\u793a\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\u8be6\u89e3"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>Vue 3\uff08\u542b Composition API\uff09\u5b8c\u6574\u793a\u4f8b + \u81ea\u52a8\u66f4\u65b0\u65f6\u95f4 + \u6587\u672c\/\u6570\u5b57\/\u683c\u5f0f\u5316\u6559\u7a0b<\/strong> \u7684<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">\u300aVue 3 \u524d\u7aef\u9879\u76ee\u5b9e\u73b0\u52a8\u6001\u663e\u793a\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\uff08\u8d85\u8be6\u7ec6\uff09\u300b<\/h1>\n\n\n\n<p>\u9002\u5408\u65b0\u624b + \u5b9e\u6218\u9879\u76ee\u53ef\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2705 \u4e00\u3001\u6700\u7b80\u5355\u7248\uff1a\u52a8\u6001\u663e\u793a\u5f53\u524d\u65f6\u95f4\uff08\u81ea\u52a8\u66f4\u65b0\uff09<\/h1>\n\n\n\n<p>\u4f7f\u7528 <code>ref<\/code> + <code>setInterval<\/code> \u5c31\u80fd\u5b9e\u65f6\u663e\u793a\u65f6\u95f4\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc <strong>\u793a\u4f8b\u4ee3\u7801\uff08Composition API\uff09<\/strong><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;template&gt;\n  &amp;lt;div class=&quot;time&quot;&gt;\n    \u5f53\u524d\u65f6\u95f4\uff1a{{ now }}\n  &amp;lt;\/div&gt;\n&amp;lt;\/template&gt;\n\n&amp;lt;script setup&gt;\nimport { ref, onMounted, onUnmounted } from &quot;vue&quot;;\n\nconst now = ref(&quot;&quot;);\n\nfunction updateTime() {\n  const date = new Date();\n  now.value = date.toLocaleString(); \/\/ 2025\/12\/05 10:20:30 \u683c\u5f0f\n}\n\nlet timer = null;\n\nonMounted(() =&gt; {\n  updateTime();\n  timer = setInterval(updateTime, 1000); \/\/ \u6bcf\u79d2\u66f4\u65b0\u4e00\u6b21\n});\n\nonUnmounted(() =&gt; {\n  clearInterval(timer);\n});\n&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<p>\ud83d\udccc <strong>\u6548\u679c\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u6bcf\u79d2\u81ea\u52a8\u5237\u65b0<\/li>\n\n\n\n<li>\u5b8c\u5168\u4e3a\u524d\u7aef\u7cfb\u7edf\u65f6\u95f4\uff0c\u4e0d\u4ece\u670d\u52a1\u5668\u83b7\u53d6<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 \u4e8c\u3001\u4f7f\u7528\u81ea\u5b9a\u4e49\u683c\u5f0f\uff08YYYY-MM-DD HH:mm:ss\uff09<\/h1>\n\n\n\n<p>\u6d4f\u89c8\u5668\u7684 <code>toLocaleString()<\/code> \u683c\u5f0f\u53ef\u80fd\u56e0\u5730\u533a\u4e0d\u540c\u800c\u53d8\u5316\uff0c\u6240\u4ee5\u5b9e\u9645\u9879\u76ee\u4e2d\u5e38\u7528\u81ea\u5b9a\u4e49\u683c\u5f0f\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc <strong>\u81ea\u5b9a\u4e49\u683c\u5f0f\u51fd\u6570<\/strong><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction formatTime(date) {\n  const y = date.getFullYear();\n  const m = String(date.getMonth() + 1).padStart(2, &quot;0&quot;);\n  const d = String(date.getDate()).padStart(2, &quot;0&quot;);\n  const hh = String(date.getHours()).padStart(2, &quot;0&quot;);\n  const mm = String(date.getMinutes()).padStart(2, &quot;0&quot;);\n  const ss = String(date.getSeconds()).padStart(2, &quot;0&quot;);\n  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udccc <strong>\u5728 Vue \u4e2d\u4f7f\u7528<\/strong><\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;template&gt;\n  &amp;lt;div&gt;\n    \u5f53\u524d\u7cfb\u7edf\u65f6\u95f4\uff1a{{ now }}\n  &amp;lt;\/div&gt;\n&amp;lt;\/template&gt;\n\n&amp;lt;script setup&gt;\nimport { ref, onMounted, onUnmounted } from &quot;vue&quot;;\n\nconst now = ref(&quot;&quot;);\n\nfunction updateTime() {\n  const date = new Date();\n  now.value = formatTime(date);\n}\n\nfunction formatTime(date) {\n  const y = date.getFullYear();\n  const m = String(date.getMonth() + 1).padStart(2, &quot;0&quot;);\n  const d = String(date.getDate()).padStart(2, &quot;0&quot;);\n  const hh = String(date.getHours()).padStart(2, &quot;0&quot;);\n  const mm = String(date.getMinutes()).padStart(2, &quot;0&quot;);\n  const ss = String(date.getSeconds()).padStart(2, &quot;0&quot;);\n  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;\n}\n\nlet timer = null;\n\nonMounted(() =&gt; {\n  updateTime(); \n  timer = setInterval(updateTime, 1000);\n});\n\nonUnmounted(() =&gt; {\n  clearInterval(timer);\n});\n&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfa8 \u4e09\u3001\u52a0\u5165\u6e10\u53d8\u52a8\u753b\uff08\u88c5\u9970\u6548\u679c\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n.time {\n  font-size: 20px;\n  color: #42b983;\n  font-weight: bold;\n  transition: all 0.3s;\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u26a1 \u56db\u3001Vue \u7ec4\u4ef6\u5c01\u88c5\uff08\u53ef\u590d\u7528\uff09<\/h1>\n\n\n\n<p>\u521b\u5efa\u7ec4\u4ef6 <code>CurrentTime.vue<\/code>\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;template&gt;\n  &amp;lt;span&gt;{{ now }}&amp;lt;\/span&gt;\n&amp;lt;\/template&gt;\n\n&amp;lt;script setup&gt;\nimport { ref, onMounted, onUnmounted } from &quot;vue&quot;;\n\nconst now = ref(&quot;&quot;);\n\nfunction format(date) {\n  return date.toLocaleString();\n}\n\nlet timer = null;\n\nonMounted(() =&gt; {\n  now.value = format(new Date());\n  timer = setInterval(() =&gt; {\n    now.value = format(new Date());\n  }, 1000);\n});\n\nonUnmounted(() =&gt; {\n  clearInterval(timer);\n});\n&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<p>\u4f7f\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;CurrentTime \/&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 \u4e94\u3001\u4f7f\u7528 Day.js\uff08\u63a8\u8350\u6700\u7b80\u5b9e\u73b0\uff09<\/h1>\n\n\n\n<p>\u82e5\u9879\u76ee\u5df2\u88c5 dayjs\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nnpm install dayjs\n\n<\/pre><\/div>\n\n\n<p>\u4ee3\u7801\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;script setup&gt;\nimport { ref, onMounted, onUnmounted } from &quot;vue&quot;;\nimport dayjs from &quot;dayjs&quot;;\n\nconst now = ref(&quot;&quot;);\n\nlet timer = null;\n\nonMounted(() =&gt; {\n  now.value = dayjs().format(&quot;YYYY-MM-DD HH:mm:ss&quot;);\n  timer = setInterval(() =&gt; {\n    now.value = dayjs().format(&quot;YYYY-MM-DD HH:mm:ss&quot;);\n  }, 1000);\n});\n\nonUnmounted(() =&gt; clearInterval(timer));\n&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2699 \u516d\u3001\u65e0\u9700 setInterval\uff08\u4f7f\u7528 requestAnimationFrame\uff09<\/h1>\n\n\n\n<p>\u5728\u9ad8\u5e27\u7387\u9875\u9762\u4e2d\u907f\u514d\u5361\u987f\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction tick() {\n  now.value = dayjs().format(&quot;YYYY-MM-DD HH:mm:ss&quot;);\n  requestAnimationFrame(tick);\n}\nrequestAnimationFrame(tick);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 \u4e03\u3001\u7ec8\u6781\u603b\u7ed3<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u4f18\u70b9<\/th><th>\u7f3a\u70b9<\/th><\/tr><\/thead><tbody><tr><td>setInterval<\/td><td>\u7b80\u5355\u3001\u5e38\u7528<\/td><td>1 \u79d2\u53ef\u80fd\u4f1a\u6709\u5fae\u5c0f\u5ef6\u8fdf<\/td><\/tr><tr><td>requestAnimationFrame<\/td><td>\u9ad8\u6027\u80fd<\/td><td>\u6bcf\u5e27\u6267\u884c\uff08\u9700\u8981\u8282\u6d41\uff09<\/td><\/tr><tr><td>dayjs \u65b9\u6848<\/td><td>\u683c\u5f0f\u5f3a\u5927\uff0c\u56fd\u9645\u5316<\/td><td>\u9700\u5b89\u88c5\u4f9d\u8d56<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\ud83d\udc49 <strong>\u63a8\u8350\uff1asetInterval + Day.js\uff08\u7b80\u5355 + \u7a33\u5b9a + \u5f3a\u5927\uff09<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd Vue 3\uff08\u542b Composition API\uff09\u5b8c\u6574\u793a\u4f8b + \u81ea\u52a8&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/05\/vue3%e5%89%8d%e7%ab%af%e9%a1%b9%e7%9b%ae%e5%ae%9e%e7%8e%b0%e5%8a%a8%e6%80%81%e6%98%be%e7%a4%ba%e5%bd%93%e5%89%8d%e7%b3%bb%e7%bb%9f%e6%97%b6%e9%97%b4%e8%af%a6%e8%a7%a3\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-524","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=524"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/524\/revisions"}],"predecessor-version":[{"id":525,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/524\/revisions\/525"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}