{"id":268,"date":"2023-06-05T15:56:31","date_gmt":"2023-06-05T07:56:31","guid":{"rendered":"https:\/\/yichenghui.net\/?p=268"},"modified":"2023-06-05T15:56:31","modified_gmt":"2023-06-05T07:56:31","slug":"vue3%e6%96%b0%e4%bd%93%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/yichenghui.net\/index.php\/2023\/06\/05\/vue3%e6%96%b0%e4%bd%93%e9%aa%8c\/","title":{"rendered":"Vue3\u65b0\u4f53\u9a8c"},"content":{"rendered":"<h2>\u7ec4\u4ef6name<\/h2>\n<p>\u5f53\u4f60\u5728\u4f7f\u7528\u5355\u6587\u4ef6\u7ec4\u4ef6\u65f6\uff0c\u7ec4\u4ef6\u5df2\u7ecf\u4f1a\u6839\u636e\u5176\u6587\u4ef6\u540d\u63a8\u5bfc\u51fa\u5176\u540d\u79f0\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4e00\u4e2a\u540d\u4e3a MyComponent.vue \u7684\u6587\u4ef6\u4f1a\u63a8\u5bfc\u51fa\u663e\u793a\u540d\u79f0\u4e3a\u201cMyComponent\u201d\u3002<\/p>\n<p>\u53e6\u4e00\u79cd\u573a\u666f\u662f\u5f53\u4e00\u4e2a\u7ec4\u4ef6\u901a\u8fc7 app.component \u88ab\u5168\u5c40\u6ce8\u518c\u65f6\uff0c\u8fd9\u4e2a\u5168\u5c40 ID \u5c31\u81ea\u52a8\u88ab\u8bbe\u4e3a\u4e86\u5176\u540d\u79f0\u3002<\/p>\n<p>\u4f7f\u7528 name \u9009\u9879\u4f7f\u4f60\u53ef\u4ee5\u8986\u76d6\u63a8\u5bfc\u51fa\u7684\u540d\u79f0\uff0c\u6216\u662f\u5728\u6ca1\u6709\u63a8\u5bfc\u51fa\u540d\u5b57\u65f6\u663e\u5f0f\u63d0\u4f9b\u4e00\u4e2a\u3002(\u4f8b\u5982\u6ca1\u6709\u4f7f\u7528\u6784\u5efa\u5de5\u5177\u65f6\uff0c\u6216\u662f\u4e00\u4e2a\u5185\u8054\u7684\u975e SFC \u5f0f\u7684\u7ec4\u4ef6)<\/p>\n<p>\u6709\u4e00\u79cd\u573a\u666f\u4e0b name \u5fc5\u987b\u662f\u5df2\u663e\u5f0f\u58f0\u660e\u7684\uff1a\u5373 <KeepAlive> \u901a\u8fc7\u5176 include \/ exclude prop \u6765\u5339\u914d\u5176\u9700\u8981\u7f13\u5b58\u7684\u7ec4\u4ef6\u65f6\u3002<\/p>\n<blockquote>\n<p>\u5728 3.2.34 \u6216\u4ee5\u4e0a\u7684\u7248\u672c\u4e2d\uff0c\u4f7f\u7528 <code><code>&lt;script setup&gt; <\/code>`<code>\u7684\u5355\u6587\u4ef6\u7ec4\u4ef6\u4f1a\u81ea\u52a8\u6839\u636e\u6587\u4ef6\u540d\u751f\u6210\u5bf9\u5e94\u7684 name \u9009\u9879\uff0c\u5373\u4f7f\u662f\u5728\u914d\u5408 <\/code>`<code>&lt;KeepAlive&gt; <\/code><\/code>\u4f7f\u7528\u65f6\u4e5f\u65e0\u9700\u518d\u624b\u52a8\u58f0\u660e\u3002<\/p>\n<\/blockquote>\n<h2>\u66b4\u9732\u516c\u5171\u5c5e\u6027<\/h2>\n<p><strong>expose<\/strong> \u51fd\u6570\u7528\u4e8e\u663e\u5f0f\u5730\u9650\u5236\u8be5\u7ec4\u4ef6\u66b4\u9732\u51fa\u7684\u5c5e\u6027\uff0c\u5f53\u7236\u7ec4\u4ef6\u901a\u8fc7\u6a21\u677f\u5f15\u7528\u8bbf\u95ee\u8be5\u7ec4\u4ef6\u7684\u5b9e\u4f8b\u65f6\uff0c\u5c06\u4ec5\u80fd\u8bbf\u95ee<strong>expose<\/strong>\u51fd\u6570\u66b4\u9732\u51fa\u7684\u5185\u5bb9\uff1a<\/p>\n<pre><code class=\"language-javascript\">export default {\n  setup(props, { expose }) {\n    \/\/ \u8ba9\u7ec4\u4ef6\u5b9e\u4f8b\u5904\u4e8e \u201c\u5173\u95ed\u72b6\u6001\u201d\n    \/\/ \u5373\u4e0d\u5411\u7236\u7ec4\u4ef6\u66b4\u9732\u4efb\u4f55\u4e1c\u897f\n    expose()\n\n    const publicCount = ref(0)\n    const privateCount = ref(0)\n    \/\/ \u6709\u9009\u62e9\u5730\u66b4\u9732\u5c40\u90e8\u72b6\u6001\n    expose({ count: publicCount })\n  }\n}<\/code><\/pre>\n<h2>script setup<\/h2>\n<p>\u8981\u542f\u7528\u8be5\u8bed\u6cd5\uff0c\u9700\u8981\u5728 <code><code>&lt;script&gt;<\/code><\/code> \u4ee3\u7801\u5757\u4e0a\u6dfb\u52a0 setup attribute\uff1a<\/p>\n<pre><code class=\"language-javascript\">&lt;script setup&gt;\nconsole.log(&#039;hello script setup&#039;)\n&lt;\/script&gt;<\/code><\/pre>\n<p>\u5f53\u4f7f\u7528 <code><code>&lt;script setup&gt; <\/code>`<code>\u7684\u65f6\u5019\uff0c\u4efb\u4f55\u5728 <\/code>`<code>&lt;script setup&gt;<\/code><\/code> \u58f0\u660e\u7684\u9876\u5c42\u7684\u7ed1\u5b9a (\u5305\u62ec\u53d8\u91cf\uff0c\u51fd\u6570\u58f0\u660e\uff0c\u4ee5\u53ca import \u5bfc\u5165\u7684\u5185\u5bb9) \u90fd\u80fd\u5728\u6a21\u677f\u4e2d\u76f4\u63a5\u4f7f\u7528\u3002import \u5bfc\u5165\u7684\u5185\u5bb9\u4e5f\u4f1a\u4ee5\u540c\u6837\u7684\u65b9\u5f0f\u66b4\u9732\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u5728\u6a21\u677f\u8868\u8fbe\u5f0f\u4e2d\u76f4\u63a5\u4f7f\u7528\u5bfc\u5165\u7684 helper \u51fd\u6570\uff0c\u800c\u4e0d\u9700\u8981\u901a\u8fc7 methods \u9009\u9879\u6765\u66b4\u9732\u5b83:<\/p>\n<pre><code class=\"language-javascript\">&lt;script setup&gt;\nimport { capitalize } from &#039;.\/helpers&#039;\n&lt;\/script&gt;\n\n&lt;template&gt;\n  &lt;div&gt;{{ capitalize(&#039;hello&#039;) }}&lt;\/div&gt;\n&lt;\/template&gt;<\/code><\/pre>\n<h2>\u5355\u6587\u4ef6\u7ec4\u4ef6CSS<\/h2>\n<h3>\u6df1\u5ea6\u9009\u62e9\u5668<\/h3>\n<p>\u5904\u4e8e <strong>scoped<\/strong> \u6837\u5f0f\u4e2d\u7684\u9009\u62e9\u5668\u5982\u679c\u60f3\u8981\u505a\u66f4\u201c\u6df1\u5ea6\u201d\u7684\u9009\u62e9\uff0c\u4e5f\u5373\uff1a\u5f71\u54cd\u5230\u5b50\u7ec4\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>:deep()<\/strong> \u8fd9\u4e2a\u4f2a\u7c7b\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style scoped&gt;\n.a :deep(.b) {\n  \/* ... *\/\n}\n&lt;\/style&gt;<\/code><\/pre>\n<p>\u4ee3\u7801\u4f1a\u88ab\u7f16\u8bd1\u6210\uff1a<\/p>\n<pre><code class=\"language-css\">.a[data-v-f3f3eg9] .b {\n  \/* ... *\/\n}<\/code><\/pre>\n<p>\u901a\u8fc7 v-html \u521b\u5efa\u7684 DOM \u5185\u5bb9\u4e0d\u4f1a\u88ab\u4f5c\u7528\u57df\u6837\u5f0f\u5f71\u54cd\uff0c\u4f46\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u6df1\u5ea6\u9009\u62e9\u5668\u6765\u8bbe\u7f6e\u5176\u6837\u5f0f\u3002<\/p>\n<h3>\u63d2\u69fd\u9009\u62e9\u5668<\/h3>\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u4f5c\u7528\u57df\u6837\u5f0f\u4e0d\u4f1a\u5f71\u54cd\u5230<code><code> &lt;slot\/&gt; <\/code><\/code>\u6e32\u67d3\u51fa\u6765\u7684\u5185\u5bb9\uff0c\u56e0\u4e3a\u5b83\u4eec\u88ab\u8ba4\u4e3a\u662f\u7236\u7ec4\u4ef6\u6240\u6301\u6709\u5e76\u4f20\u9012\u8fdb\u6765\u7684\u3002\u4f7f\u7528 <strong>:slotted<\/strong> \u4f2a\u7c7b\u4ee5\u660e\u786e\u5730\u5c06\u63d2\u69fd\u5185\u5bb9\u4f5c\u4e3a\u9009\u62e9\u5668\u7684\u76ee\u6807\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style scoped&gt;\n:slotted(div) {\n  color: red;\n}\n&lt;\/style&gt;<\/code><\/pre>\n<h3>\u5168\u5c40\u9009\u62e9\u5668<\/h3>\n<p>\u5982\u679c\u60f3\u8ba9\u5176\u4e2d\u4e00\u4e2a\u6837\u5f0f\u89c4\u5219\u5e94\u7528\u5230\u5168\u5c40\uff0c\u6bd4\u8d77\u53e6\u5916\u521b\u5efa\u4e00\u4e2a <code><code>&lt;style&gt;<\/code><\/code>\uff0c\u53ef\u4ee5\u4f7f\u7528 :global \u4f2a\u7c7b\u6765\u5b9e\u73b0 (\u770b\u4e0b\u9762\u7684\u4ee3\u7801)\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;style scoped&gt;\n:global(.red) {\n  color: red;\n}\n&lt;\/style&gt;<\/code><\/pre>\n<h3>CSS Modules<\/h3>\n<p>\u4e00\u4e2a <code><code>&lt;style module&gt; <\/code><\/code>\u6807\u7b7e\u4f1a\u88ab\u7f16\u8bd1\u4e3a CSS Modules \u5e76\u4e14\u5c06\u751f\u6210\u7684 CSS class \u4f5c\u4e3a $style \u5bf9\u8c61\u66b4\u9732\u7ed9\u7ec4\u4ef6\uff1a<\/p>\n<pre><code class=\"language-html\">&lt;template&gt;\n  &lt;p :class=&quot;$style.red&quot;&gt;This should be red&lt;\/p&gt;\n&lt;\/template&gt;\n\n&lt;style module&gt;\n.red {\n  color: red;\n}\n&lt;\/style&gt;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ec4\u4ef6name \u5f53\u4f60\u5728\u4f7f\u7528\u5355\u6587\u4ef6\u7ec4\u4ef6\u65f6\uff0c\u7ec4\u4ef6\u5df2\u7ecf\u4f1a\u6839\u636e\u5176\u6587\u4ef6\u540d\u63a8\u5bfc\u51fa\u5176\u540d\u79f0\u3002\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4e00\u4e2a\u540d\u4e3a MyCompon &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/yichenghui.net\/index.php\/2023\/06\/05\/vue3%e6%96%b0%e4%bd%93%e9%aa%8c\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cVue3\u65b0\u4f53\u9a8c\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kadence_starter_templates_imported_post":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-268","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/posts\/268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/comments?post=268"}],"version-history":[{"count":0,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/posts\/268\/revisions"}],"wp:attachment":[{"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/media?parent=268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/categories?post=268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yichenghui.net\/index.php\/wp-json\/wp\/v2\/tags?post=268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}