{"id":1216,"date":"2024-04-11T09:23:00","date_gmt":"2024-04-11T01:23:00","guid":{"rendered":"http:\/\/ericw.top\/?p=1216"},"modified":"2024-04-24T08:47:36","modified_gmt":"2024-04-24T00:47:36","slug":"%e7%bb%84%e5%90%88%e5%bc%8fapi","status":"publish","type":"post","link":"http:\/\/ericw.top\/?p=1216","title":{"rendered":"\u7ec4\u5408\u5f0fAPI"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"toc-0\">setup\u9009\u9879<\/h2>\n\n\n\n<p>1\u3001setup\u9009\u9879\u7684\u5199\u6cd5\u548c\u6267\u884c\u65f6\u673a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script&gt;\n  export default {\n    setup(){\n      \n    },\n    beforeCreate(){\n      \n    }\n  }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-b11808d5cfac3a1aa94c47c062b328ab\">\u4ed6\u7684\u6267\u884c\u65f6\u673a\u662f\u5728<code class=\"prettyprint\" >beforeCreate<\/code>\u94a9\u5b50\u51fd\u6570\u4e4b\u524d\u7684<\/p>\n\n\n\n<p>2\u3001setup\u4e2d\u7684\u4ee3\u7801\u7279\u70b9<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script&gt;\n  export default {\n    setup(){\n      const message = &#039;this is message&#039;\n      const logMessage = ()=&gt;{\n        console.log(message)\n      }\n      \/\/ \u5fc5\u987breturn\u624d\u53ef\u4ee5\n      return {\n        message,\n        logMessage\n      }\n    }\n  }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-c163f08c97052092787578de0322c30c\">\u5728setup\u51fd\u6570\u4e2d\u5199\u7684\u6570\u636e\u548c\u65b9\u6cd5\u9700\u8981\u5728\u672b\u5c3e\u4ee5\u5bf9\u8c61\u7684\u65b9\u5f0f<code class=\"prettyprint\" >return<\/code>\uff0c\u624d\u80fd\u7ed9\u6a21\u7248\u4f7f\u7528<\/p>\n\n\n\n<p>3\u3001<code class=\"prettyprint\" >&lt;script setup&gt;<\/code>\u8bed\u6cd5\u7cd6<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-83fb120aece86f3f57ebdbe522ef6f2c\">script\u6807\u7b7e\u6dfb\u52a0 setup\u6807\u8bb0\uff0c\u4e0d\u9700\u8981\u518d\u5199\u5bfc\u51fa\u8bed\u53e5\uff0c\u9ed8\u8ba4\u4f1a\u6dfb\u52a0\u5bfc\u51fa\u8bed\u53e5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script setup&gt;\n  const message = &#039;this is message&#039;\n  const logMessage = ()=&gt;{\n    console.log(message)\n  }\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-1\">reactive\u548cref\u51fd\u6570<\/h2>\n\n\n\n<p>1\u3001reactive<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-0d6236d26ace832215570e4a0c8a38b1\">\u63a5\u53d7\u5bf9\u8c61\u7c7b\u578b\u6570\u636e\u7684\u53c2\u6570\u4f20\u5165\u5e76\u8fd4\u56de\u4e00\u4e2a\u54cd\u5e94\u5f0f\u7684\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script setup&gt;\n \/\/ \u5bfc\u5165\n import { reactive } from &#039;vue&#039;\n \/\/ \u6267\u884c\u51fd\u6570 \u4f20\u5165\u53c2\u6570 \u53d8\u91cf\u63a5\u6536\n const state = reactive({\n   msg:&#039;this is msg&#039;\n })\n const setSate = ()=&gt;{\n   \/\/ \u4fee\u6539\u6570\u636e\u66f4\u65b0\u89c6\u56fe\n   state.msg = &#039;this is new msg&#039;\n }\n&lt;\/script&gt;\n\n&lt;template&gt;\n  {{ state.msg }}\n  &lt;button @click=&quot;setState&quot;&gt;change msg&lt;\/button&gt;\n&lt;\/template&gt;<\/code><\/pre>\n\n\n\n<p>2\u3001ref<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-eb13cb2ef5379ca752e663a7ba5952b9\">\u63a5\u6536\u7b80\u5355\u7c7b\u578b\u6216\u8005\u5bf9\u8c61\u7c7b\u578b\u7684\u6570\u636e\u4f20\u5165\u5e76\u8fd4\u56de\u4e00\u4e2a\u54cd\u5e94\u5f0f\u7684\u5bf9\u8c61<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script setup&gt;\n \/\/ \u5bfc\u5165\n import { ref } from &#039;vue&#039;\n \/\/ \u6267\u884c\u51fd\u6570 \u4f20\u5165\u53c2\u6570 \u53d8\u91cf\u63a5\u6536\n const count = ref(0)\n const setCount = ()=&gt;{\n   \/\/ \u4fee\u6539\u6570\u636e\u66f4\u65b0\u89c6\u56fe\u5fc5\u987b\u52a0\u4e0a.value\n   count.value++\n }\n&lt;\/script&gt;\n\n&lt;template&gt;\n  &lt;button @click=&quot;setCount&quot;&gt;{{count}}&lt;\/button&gt;\n&lt;\/template&gt;<\/code><\/pre>\n\n\n\n<p>3\u3001reactive\u5bf9\u6bd4ref<\/p>\n\n\n\n<ol>\n<li>\u90fd\u662f\u7528\u6765<strong>\u751f\u6210\u54cd\u5e94\u5f0f\u6570\u636e<\/strong><\/li>\n\n\n\n<li>\u4e0d\u540c\u70b9\n<ol>\n<li><code class=\"prettyprint\" >reactive<\/code>\u4e0d\u80fd\u5904\u7406\u7b80\u5355\u7c7b\u578b\u7684\u6570\u636e<\/li>\n\n\n\n<li><code class=\"prettyprint\" >ref<\/code>\u53c2\u6570\u7c7b\u578b\u652f\u6301\u66f4\u597d\uff0c\u4f46\u662f\u5fc5\u987b\u901a\u8fc7<code class=\"prettyprint\" >.value<\/code>\u505a\u8bbf\u95ee\u4fee\u6539<\/li>\n\n\n\n<li><code class=\"prettyprint\" >ref<\/code>\u51fd\u6570\u5185\u90e8\u7684\u5b9e\u73b0\u4f9d\u8d56\u4e8e<code class=\"prettyprint\" >reactive<\/code>\u51fd\u6570<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>\u5728\u5b9e\u9645\u5de5\u4f5c\u4e2d\u7684\u63a8\u8350\n<ol>\n<li>\u63a8\u8350\u4f7f\u7528<code class=\"prettyprint\" >ref<\/code>\u51fd\u6570\uff0c\u51cf\u5c11\u8bb0\u5fc6\u8d1f\u62c5\uff0c\u5c0f\u5154\u9c9c\u9879\u76ee\u90fd\u4f7f\u7528<code class=\"prettyprint\" >ref<\/code><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-2\">computed<\/h2>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-ac2d5e275c0286be15dabb5b5d3b0298\">\u8ba1\u7b97\u5c5e\u6027\u57fa\u672c\u601d\u60f3\u548cVue2\u4fdd\u6301\u4e00\u81f4\uff0c\u7ec4\u5408\u5f0fAPI\u4e0b\u7684\u8ba1\u7b97\u5c5e\u6027\u53ea\u662f\u4fee\u6539\u4e86API\u5199\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script setup&gt;\n\/\/ \u5bfc\u5165\nimport {ref, computed } from &#039;vue&#039;\n\/\/ \u539f\u59cb\u6570\u636e\nconst count = ref(0)\n\/\/ \u8ba1\u7b97\u5c5e\u6027\nconst doubleCount = computed(()=&gt;count.value * 2)\n\n\/\/ \u539f\u59cb\u6570\u636e\nconst list = ref(&#091;1,2,3,4,5,6,7,8])\n\/\/ \u8ba1\u7b97\u5c5e\u6027list\nconst filterList = computed(item=&gt;item &gt; 2)\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-3\">watch<\/h2>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-6a33686cbbc5cb731d9c122221e88963\">\u4fa6\u542c\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u6570\u636e\u7684\u53d8\u5316\uff0c\u6570\u636e\u53d8\u5316\u65f6\u6267\u884c\u56de\u8c03\u51fd\u6570\uff0c\u4fe9\u4e2a\u989d\u5916\u53c2\u6570 immediate\u63a7\u5236\u7acb\u523b\u6267\u884c\uff0cdeep\u5f00\u542f\u6df1\u5ea6\u4fa6\u542c<\/p>\n\n\n\n<p>1\u3001\u4fa6\u542c\u5355\u4e2a\u6570\u636e<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script setup&gt;\n  \/\/ 1. \u5bfc\u5165watch\n  import { ref, watch } from &#039;vue&#039;\n  const count = ref(0)\n  \/\/ 2. \u8c03\u7528watch \u4fa6\u542c\u53d8\u5316\n  watch(count, (newValue, oldValue)=&gt;{\n    console.log(`count\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u8001\u503c\u4e3a${oldValue},\u65b0\u503c\u4e3a${newValue}`)\n  })\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>2\u3001\u4fa6\u542c\u591a\u4e2a\u6570\u636e<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-3f2117eadc1d247df899e56eb1400e61\">\u4fa6\u542c\u591a\u4e2a\u6570\u636e\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u53ef\u4ee5\u6539\u5199\u6210\u6570\u7ec4\u7684\u5199\u6cd5<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script setup&gt;\n  \/\/ 1. \u5bfc\u5165watch\n  import { ref, watch } from &#039;vue&#039;\n  const count = ref(0)\n  const name = ref(&#039;cp&#039;)\n  \/\/ 2. \u8c03\u7528watch \u4fa6\u542c\u53d8\u5316\n  watch(&#091;count, name], (&#091;newCount, newName],&#091;oldCount,oldName])=&gt;{\n    console.log(`count\u6216\u8005name\u53d8\u5316\u4e86\uff0c&#091;newCount, newName],&#091;oldCount,oldName])\n  })\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>3\u3001immediate<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-5f1d1eeffe6f4101fb915611be020a23\">\u5728\u4fa6\u542c\u5668\u521b\u5efa\u65f6\u7acb\u5373\u51fa\u53d1\u56de\u8c03\uff0c\u54cd\u5e94\u5f0f\u6570\u636e\u53d8\u5316\u4e4b\u540e\u7ee7\u7eed\u6267\u884c\u56de\u8c03<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script setup&gt;\n  \/\/ 1. \u5bfc\u5165watch\n  import { ref, watch } from &#039;vue&#039;\n  const count = ref(0)\n  \/\/ 2. \u8c03\u7528watch \u4fa6\u542c\u53d8\u5316\n  watch(count, (newValue, oldValue)=&gt;{\n    console.log(`count\u53d1\u751f\u4e86\u53d8\u5316\uff0c\u8001\u503c\u4e3a${oldValue},\u65b0\u503c\u4e3a${newValue}`)\n  },{\n    immediate: true\n  })\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>4\u3001deep<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-09352fc132a060c5843fb4b1b5b192fa\">\u901a\u8fc7watch\u76d1\u542c\u7684ref\u5bf9\u8c61\u9ed8\u8ba4\u662f\u6d45\u5c42\u4fa6\u542c\u7684\uff0c\u76f4\u63a5\u4fee\u6539\u5d4c\u5957\u7684\u5bf9\u8c61\u5c5e\u6027\u4e0d\u4f1a\u89e6\u53d1\u56de\u8c03\u6267\u884c\uff0c\u9700\u8981\u5f00\u542fdeep<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;script setup&gt;\n  \/\/ 1. \u5bfc\u5165watch\n  import { ref, watch } from &#039;vue&#039;\n  const state = ref({ count: 0 })\n  \/\/ 2. \u76d1\u542c\u5bf9\u8c61state\n  watch(state, ()=&gt;{\n    console.log(&#039;\u6570\u636e\u53d8\u5316\u4e86&#039;)\n  })\n  const changeStateByCount = ()=&gt;{\n    \/\/ \u76f4\u63a5\u4fee\u6539\u4e0d\u4f1a\u5f15\u53d1\u56de\u8c03\u6267\u884c\n    state.value.count++\n  }\n&lt;\/script&gt;\n\n&lt;script setup&gt;\n  \/\/ 1. \u5bfc\u5165watch\n  import { ref, watch } from &#039;vue&#039;\n  const state = ref({ count: 0 })\n  \/\/ 2. \u76d1\u542c\u5bf9\u8c61state \u5e76\u5f00\u542fdeep\n  watch(state, ()=&gt;{\n    console.log(&#039;\u6570\u636e\u53d8\u5316\u4e86&#039;)\n  },{deep:true})\n  const changeStateByCount = ()=&gt;{\n    \/\/ \u6b64\u65f6\u4fee\u6539\u53ef\u4ee5\u89e6\u53d1\u56de\u8c03\n    state.value.count++\n  }\n&lt;\/script&gt;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-4\">\u751f\u547d\u5468\u671f\u51fd\u6570<\/h2>\n\n\n\n<p>1\u3001\u9009\u9879\u5f0f\u5bf9\u6bd4\u7ec4\u5408\u5f0f<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"498\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-18-1024x498.png\" alt=\"\" class=\"wp-image-1217\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-18-1024x498.png 1024w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-18-300x146.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-18-768x374.png 768w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-18-1536x747.png 1536w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-18.png 1562w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>2\u3001\u751f\u547d\u5468\u671f\u51fd\u6570\u57fa\u672c\u4f7f\u7528<\/p>\n\n\n\n<ol>\n<li>\u5bfc\u5165\u751f\u547d\u5468\u671f\u51fd\u6570<\/li>\n\n\n\n<li>\u6267\u884c\u751f\u547d\u5468\u671f\u51fd\u6570\uff0c\u4f20\u5165\u56de\u8c03<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;scirpt setup&gt;\nimport { onMounted } from &#039;vue&#039;\nonMounted(()=&gt;{\n  \/\/ \u81ea\u5b9a\u4e49\u903b\u8f91\n})\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<p>3\u3001\u6267\u884c\u591a\u6b21<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-c8cd7ab923b0ed998898220a02787a53\">\u751f\u547d\u5468\u671f\u51fd\u6570\u6267\u884c\u591a\u6b21\u7684\u65f6\u5019\uff0c\u4f1a\u6309\u7167\u987a\u5e8f\u4f9d\u6b21\u6267\u884c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >&lt;scirpt setup&gt;\nimport { onMounted } from &#039;vue&#039;\nonMounted(()=&gt;{\n  \/\/ \u81ea\u5b9a\u4e49\u903b\u8f91\n})\n\nonMounted(()=&gt;{\n  \/\/ \u81ea\u5b9a\u4e49\u903b\u8f91\n})\n&lt;\/script&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-5\">\u7236\u5b50\u901a\u4fe1<\/h2>\n\n\n\n<p>1\u3001\u7236\u4f20\u5b50<\/p>\n\n\n\n<ol>\n<li>\u7236\u7ec4\u4ef6\u4e2d\u7ed9\u5b50\u7ec4\u4ef6\u7ed1\u5b9a\u5c5e\u6027<\/li>\n\n\n\n<li>\u5b50\u7ec4\u4ef6\u5185\u90e8\u901a\u8fc7props\u9009\u9879\u63a5\u6536\u6570\u636e<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"299\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-19-1024x299.png\" alt=\"\" class=\"wp-image-1218\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-19-1024x299.png 1024w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-19-300x88.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-19-768x224.png 768w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-19-1536x448.png 1536w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-19.png 1608w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>2\u3001\u5b50\u4f20\u7236<\/p>\n\n\n\n<ol>\n<li>\u7236\u7ec4\u4ef6\u4e2d\u7ed9\u5b50\u7ec4\u4ef6\u6807\u7b7e\u901a\u8fc7@\u7ed1\u5b9a\u4e8b\u4ef6<\/li>\n\n\n\n<li>\u5b50\u7ec4\u4ef6\u5185\u90e8\u901a\u8fc7 emit \u65b9\u6cd5\u89e6\u53d1\u4e8b\u4ef6<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"361\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-20-1024x361.png\" alt=\"\" class=\"wp-image-1219\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-20-1024x361.png 1024w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-20-300x106.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-20-768x271.png 768w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-20-1536x541.png 1536w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-20.png 1595w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-6\">\u6a21\u7248\u5f15\u7528<\/h2>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-7bc204012802662830b8efd8d96c7f3a\">\u6982\u5ff5\uff1a\u901a\u8fc7 ref\u6807\u8bc6 \u83b7\u53d6\u771f\u5b9e\u7684 dom\u5bf9\u8c61\u6216\u8005\u7ec4\u4ef6\u5b9e\u4f8b\u5bf9\u8c61<\/p>\n\n\n\n<p>1\u3001\u57fa\u672c\u4f7f\u7528<\/p>\n\n\n\n<p>\u5b9e\u73b0\u6b65\u9aa4\uff1a<br>1.\u8c03\u7528ref\u51fd\u6570\u751f\u6210\u4e00\u4e2aref\u5bf9\u8c61<br>2.\u901a\u8fc7ref\u8868\u793a\u7ed1\u5b9aref\u5bf9\u8c61\u5230\u6807\u7b7e<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"827\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-21-1024x827.png\" alt=\"\" class=\"wp-image-1220\" style=\"width:449px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-21-1024x827.png 1024w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-21-300x242.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-21-768x620.png 768w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-21-1536x1241.png 1536w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-21.png 1613w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>2\u3001defineExpose<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-45bd4aa7a8e13d6528d0b369f06ecc0a\">\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5728<code class=\"prettyprint\" > &lt;script setup&gt;<\/code>\u8bed\u6cd5\u7cd6\u4e0b\u7ec4\u4ef6\u5185\u90e8\u7684\u5c5e\u6027\u548c\u65b9\u6cd5\u662f\u4e0d\u5f00\u653e\u7ed9\u7236\u7ec4\u4ef6\u8bbf\u95ee\u7684\uff0c\u53ef\u4ee5\u901a\u8fc7<code class=\"prettyprint\" >defineExpose<\/code>\u7f16\u8bd1\u5b8f\u6307\u5b9a\u54ea\u4e9b\u5c5e\u6027\u548c\u65b9\u6cd5\u5bb9\u8bb8\u8bbf\u95ee \u8bf4\u660e\uff1a\u6307\u5b9a<code class=\"prettyprint\" >testMessage<\/code>\u5c5e\u6027\u53ef\u4ee5\u88ab\u8bbf\u95ee\u5230<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"toc-7\">provide\u548cinject<\/h2>\n\n\n\n<p>1\u3001\u4f5c\u7528\u4e0e\u573a\u666f<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-9f3a4021fee6a1480e130a88a905336b\">\u9876\u5c42\u7ec4\u4ef6\u5411\u4efb\u610f\u7684\u5e95\u5c42\u7ec4\u4ef6\u4f20\u9012\u6570\u636e\u548c\u65b9\u6cd5\uff0c\u5b9e\u73b0\u8de8\u5c42\u7ec4\u4ef6\u901a\u4fe1<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"476\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-22-1024x476.png\" alt=\"\" class=\"wp-image-1221\" style=\"width:596px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-22-1024x476.png 1024w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-22-300x139.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-22-768x357.png 768w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-22-1536x714.png 1536w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-22.png 1567w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>2\u3001\u8de8\u5c42\u4f20\u9012\u666e\u901a\u6570\u636e<\/p>\n\n\n\n<ol>\n<li>\u9876\u5c42\u7ec4\u4ef6\u901a\u8fc7 <code class=\"prettyprint\" >provide<\/code> \u51fd\u6570\u63d0\u4f9b\u6570\u636e<\/li>\n\n\n\n<li>\u5e95\u5c42\u7ec4\u4ef6\u901a\u8fc7 <code class=\"prettyprint\" >inject<\/code> \u51fd\u6570\u63d0\u4f9b\u6570\u636e<\/li>\n<\/ol>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"667\" height=\"336\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-23.png\" alt=\"\" class=\"wp-image-1222\" style=\"width:467px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-23.png 667w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-23-300x151.png 300w\" sizes=\"(max-width: 667px) 100vw, 667px\" \/><\/figure>\n\n\n\n<p>3\u3001\u8de8\u5c42\u4f20\u9012\u54cd\u5e94\u5f0f\u6570\u636e<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-e5c6182c5c89220192ae1042d5b98eb3\">\u5728\u8c03\u7528provide\u51fd\u6570\u65f6\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8bbe\u7f6e\u4e3aref\u5bf9\u8c61<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"676\" height=\"341\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-24.png\" alt=\"\" class=\"wp-image-1223\" style=\"width:470px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-24.png 676w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-24-300x151.png 300w\" sizes=\"(max-width: 676px) 100vw, 676px\" \/><\/figure>\n\n\n\n<p>4\u3001\u8de8\u5c42\u4f20\u9012\u65b9\u6cd5<\/p>\n\n\n\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-small-font-size wp-elements-3c0a931001959a1951feb5f181a8070d\">\u9876\u5c42\u7ec4\u4ef6\u53ef\u4ee5\u5411\u5e95\u5c42\u7ec4\u4ef6\u4f20\u9012\u65b9\u6cd5\uff0c\u5e95\u5c42\u7ec4\u4ef6\u8c03\u7528\u65b9\u6cd5\u4fee\u6539\u9876\u5c42\u7ec4\u4ef6\u7684\u6570\u636e<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"281\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-25-1024x281.png\" alt=\"\" class=\"wp-image-1224\" style=\"width:733px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-25-1024x281.png 1024w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-25-300x82.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-25-768x211.png 768w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-25-1536x422.png 1536w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-25.png 1687w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"toc-8\">Vue3.3 \u65b0\u7279\u6027-defineOptions<\/h1>\n\n\n\n<p>\u80cc\u666f\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p>\u6709 &lt;script setup&gt; \u4e4b\u524d\uff0c\u5982\u679c\u8981\u5b9a\u4e49 props, emits \u53ef\u4ee5\u8f7b\u800c\u6613\u4e3e\u5730\u6dfb\u52a0\u4e00\u4e2a\u4e0e setup \u5e73\u7ea7\u7684\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>\u4f46\u662f\u7528\u4e86 &lt;script setup&gt; \u540e\uff0c\u5c31\u6ca1\u6cd5\u8fd9\u4e48\u5e72\u4e86 setup \u5c5e\u6027\u5df2\u7ecf\u6ca1\u6709\u4e86\uff0c\u81ea\u7136\u65e0\u6cd5\u6dfb\u52a0\u4e0e\u5176\u5e73\u7ea7\u7684\u5c5e\u6027\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\uff0c\u5f15\u5165\u4e86 defineProps \u4e0e defineEmits \u8fd9\u4e24\u4e2a\u5b8f\u3002\u4f46\u8fd9\u53ea\u89e3\u51b3\u4e86 props \u4e0e emits \u8fd9\u4e24\u4e2a\u5c5e\u6027\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6211\u4eec\u8981\u5b9a\u4e49\u7ec4\u4ef6\u7684 name \u6216\u5176\u4ed6\u81ea\u5b9a\u4e49\u7684\u5c5e\u6027\uff0c\u8fd8\u662f\u5f97\u56de\u5230\u6700\u539f\u59cb\u7684\u7528\u6cd5\u2014\u2014\u518d\u6dfb\u52a0\u4e00\u4e2a\u666e\u901a\u7684 &lt;script&gt; \u6807\u7b7e\u3002<\/p>\n\n\n\n<p>\u8fd9\u6837\u5c31\u4f1a\u5b58\u5728\u4e24\u4e2a &lt;script&gt; \u6807\u7b7e\u3002\u8ba9\u4eba\u65e0\u6cd5\u63a5\u53d7\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u6240\u4ee5\u5728 Vue 3.3 \u4e2d\u65b0\u5f15\u5165\u4e86 defineOptions \u5b8f\u3002\u987e\u540d\u601d\u4e49\uff0c\u4e3b\u8981\u662f\u7528\u6765\u5b9a\u4e49 Options API \u7684\u9009\u9879\u3002\u53ef\u4ee5\u7528 defineOptions \u5b9a\u4e49\u4efb\u610f\u7684\u9009\u9879\uff0c props, emits, expose, slots \u9664\u5916\uff08\u56e0\u4e3a\u8fd9\u4e9b\u53ef\u4ee5\u4f7f\u7528 defineXXX \u6765\u505a\u5230\uff09<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"802\" height=\"343\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-36.png\" alt=\"\" class=\"wp-image-1253\" style=\"width:526px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-36.png 802w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-36-300x128.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-36-768x328.png 768w\" sizes=\"(max-width: 802px) 100vw, 802px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"toc-9\">Vue3.3\u65b0\u7279\u6027-defineModel<\/h1>\n\n\n\n<p>\u5728Vue3\u4e2d\uff0c\u81ea\u5b9a\u4e49\u7ec4\u4ef6\u4e0a\u4f7f\u7528v-model, \u76f8\u5f53\u4e8e\u4f20\u9012\u4e00\u4e2amodelValue\u5c5e\u6027\uff0c\u540c\u65f6\u89e6\u53d1 update:modelValue \u4e8b\u4ef6<\/p>\n\n\n\n<p>\u6211\u4eec\u9700\u8981\u5148\u5b9a\u4e49 props\uff0c\u518d\u5b9a\u4e49 emits \u3002\u5176\u4e2d\u6709\u8bb8\u591a\u91cd\u590d\u7684\u4ee3\u7801\u3002\u5982\u679c\u9700\u8981\u4fee\u6539\u6b64\u503c\uff0c\u8fd8\u9700\u8981\u624b\u52a8\u8c03\u7528 emit \u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f\u4e4e defineModel \u8bde\u751f\u4e86\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"217\" src=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-35-1024x217.png\" alt=\"\" class=\"wp-image-1251\" style=\"width:599px;height:auto\" srcset=\"http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-35-1024x217.png 1024w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-35-300x64.png 300w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-35-768x163.png 768w, http:\/\/ericw.top\/wp-content\/uploads\/2024\/04\/image-35.png 1130w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u751f\u6548\u9700\u8981\u914d\u7f6e vite.config.js<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"prettyprint\" >import { fileURLToPath, URL } from &#039;node:url&#039;\nimport { defineConfig } from &#039;vite&#039;\nimport vue from &#039;@vitejs\/plugin-vue&#039;\n\n\/\/ https:\/\/vitejs.dev\/config\/\nexport default defineConfig({\n  plugins: &#091;\n    vue({\n      script: {\n        defineModel: true\n      }\n    }),\n  ],\n  resolve: {\n    alias: {\n      &#039;@&#039;: fileURLToPath(new URL(&#039;.\/src&#039;, import.meta.url))\n    }\n  }\n})<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>setup\u9009\u9879 1\u3001setup\u9009\u9879\u7684\u5199\u6cd5\u548c\u6267\u884c\u65f6\u673a \u4ed6\u7684\u6267\u884c\u65f6\u673a\u662f\u5728beforeCreate\u94a9\u5b50\u51fd\u6570\u4e4b\u524d\u7684 2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1225,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"_links":{"self":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/1216"}],"collection":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1216"}],"version-history":[{"count":6,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/1216\/revisions"}],"predecessor-version":[{"id":1258,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/posts\/1216\/revisions\/1258"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=\/wp\/v2\/media\/1225"}],"wp:attachment":[{"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1216"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ericw.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}