{"id":167,"date":"2022-03-04T08:17:46","date_gmt":"2022-03-04T00:17:46","guid":{"rendered":"http:\/\/www.hgostand.com\/?p=167"},"modified":"2022-03-23T15:28:43","modified_gmt":"2022-03-23T07:28:43","slug":"vue3-10-%e7%a7%8d%e7%bb%84%e4%bb%b6%e9%80%9a%e8%ae%af%e6%96%b9%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/wite.oxoooo.com\/?p=167","title":{"rendered":"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u4f17\u6240\u5468\u77e5\uff0c<code>Vue.js<\/code> \u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\u662f\u7ec4\u4ef6\u901a\u4fe1\uff0c\u4e0d\u7ba1\u662f\u4e1a\u52a1\u7c7b\u7684\u5f00\u53d1\u8fd8\u662f\u7ec4\u4ef6\u5e93\u5f00\u53d1\uff0c\u90fd\u6709\u5404\u81ea\u7684\u901a\u8baf\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u9002\u5408\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\u6709 <code>Vue 3<\/code> \u57fa\u7840\u7684\u8bfb\u8005\u3002<\/li><li>\u6253\u7b97\u5f00\u53d1\u7ec4\u4ef6\u5e93\u7684\u8bfb\u8005\u3002<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;br&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u4f1a\u6d89\u53ca\u7684\u77e5\u8bc6\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Props<\/li><li>emits<\/li><li>expose \/ ref<\/li><li>Non-Props<\/li><li>v-model<\/li><li>\u63d2\u69fd slot<\/li><li>provide \/ inject<\/li><li>\u603b\u7ebf bus<\/li><li>getCurrentInstance<\/li><li>Vuex<\/li><li>Pinia<\/li><li>mitt.js<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4f1a\u5c06\u4e0a\u9762\u7f57\u5217\u7684\u77e5\u8bc6\u70b9\u90fd\u5199\u4e00\u4e2a\u7b80\u5355\u7684 demo\u3002\u672c\u6587\u7684\u76ee\u7684\u662f\u8ba9\u5927\u5bb6\u77e5\u9053\u6709\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u7528\uff0c\u6240\u4ee5\u5e76\u4e0d\u4f1a\u6df1\u6316\u6bcf\u4e2a\u77e5\u8bc6\u70b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5efa\u8bae\u8bfb\u8005\u8ddf\u7740\u672c\u6587\u6572\u4e00\u904d\u4ee3\u7801\uff0c\u7136\u540e\u6839\u636e\u672c\u6587\u7ed9\u51fa\u7684\u94fe\u63a5\u53bb\u6df1\u6316\u5404\u4e2a\u77e5\u8bc6\u70b9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6536\u85cf(\u5b66\u5230)\u662f\u81ea\u5df1\u7684\uff01<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Props<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7236\u7ec4\u4ef6\u4f20\u503c\u7ed9\u5b50\u7ec4\u4ef6\uff08\u7b80\u79f0\uff1a\u7236\u4f20\u5b50\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-props.html\">Props \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;!-- \u4f7f\u7528\u5b50\u7ec4\u4ef6 --&gt;<\/code><code>  &lt;Child :msg=\"message\" \/&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import Child from '.\/components\/Child.vue' \/\/ \u5f15\u5165\u5b50\u7ec4\u4ef6<\/code><code><br><\/code><code>let message = '\u96f7\u7334'<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    {{ msg }}<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code><br><\/code><code>const props = defineProps({<\/code><code>  msg: {<\/code><code>    type: String,<\/code><code>    default: ''<\/code><code>  }<\/code><code>})<\/code><code><br><\/code><code>console.log(props.msg) \/\/ \u5728 js \u91cc\u9700\u8981\u4f7f\u7528 props.xxx \u7684\u65b9\u5f0f\u4f7f\u7528\u3002\u5728 html \u4e2d\u4f7f\u7528\u4e0d\u9700\u8981 props<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>&lt;script setup&gt;<\/code> \u4e2d\u5fc5\u987b\u4f7f\u7528 <code>defineProps<\/code> API \u6765\u58f0\u660e <code>props<\/code>\uff0c\u5b83\u5177\u5907\u5b8c\u6574\u7684\u63a8\u65ad\u5e76\u4e14\u5728 <code>&lt;script setup&gt;<\/code> \u4e2d\u662f\u76f4\u63a5\u53ef\u7528\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u66f4\u591a\u7ec6\u8282\u8bf7\u770b <a href=\"https:\/\/v3.cn.vuejs.org\/api\/sfc-script-setup.html#defineprops-%E5%92%8C-defineemits\">\u6587\u6863<\/a>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>&lt;script setup&gt;<\/code> \u4e2d\uff0c<code>defineProps<\/code> \u4e0d\u9700\u8981\u53e6\u5916\u5f15\u5165\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>props<\/code> \u5176\u5b9e\u8fd8\u80fd\u505a\u5f88\u591a\u4e8b\u60c5\uff0c\u6bd4\u5982\uff1a\u8bbe\u7f6e\u9ed8\u8ba4\u503c <code>default<\/code> \uff0c\u7c7b\u578b\u9a8c\u8bc1 <code>type<\/code> \uff0c\u8981\u6c42\u5fc5\u4f20 <code>required<\/code> \uff0c\u81ea\u5b9a\u4e49\u9a8c\u8bc1\u51fd\u6570 <code>validator<\/code> \u7b49\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u5bb6\u53ef\u4ee5\u53bb\u5b98\u7f51\u770b\u770b\uff0c\u8fd9\u662f<strong>\u5fc5\u987b\u638c\u63e1<\/strong>\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-props.html\">props \u6587\u6863<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">emits<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u7ec4\u4ef6\u901a\u77e5\u7236\u7ec4\u4ef6\u89e6\u53d1\u4e00\u4e2a\u4e8b\u4ef6\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f20\u503c\u7ed9\u7236\u7ec4\u4ef6\u3002\uff08\u7b80\u79f0\uff1a\u5b50\u4f20\u7236\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/migration\/emits-option.html\">emits \u6587\u6863<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/4e\/4e69b2ea45d4e5e5380b76a1a3a35f3e.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;\u7236\u7ec4\u4ef6\uff1a{{ message }}&lt;\/div&gt;<\/code><code>  &lt;!-- \u81ea\u5b9a\u4e49 changeMsg \u4e8b\u4ef6 --&gt;<\/code><code>  &lt;Child @changeMsg=\"changeMessage\" \/&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code><br><\/code><code>let message = ref('\u96f7\u7334')<\/code><code><br><\/code><code>\/\/ \u66f4\u6539 message \u7684\u503c\uff0cdata\u662f\u4ece\u5b50\u7ec4\u4ef6\u4f20\u8fc7\u6765\u7684<\/code><code>function changeMessage(data) {<\/code><code>  message.value = data<\/code><code>}<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    \u5b50\u7ec4\u4ef6\uff1a&lt;button @click=\"handleClick\"&gt;\u5b50\u7ec4\u4ef6\u7684\u6309\u94ae&lt;\/button&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code><br><\/code><code>\/\/ \u6ce8\u518c\u4e00\u4e2a\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u540d\uff0c\u5411\u4e0a\u4f20\u9012\u65f6\u544a\u8bc9\u7236\u7ec4\u4ef6\u8981\u89e6\u53d1\u7684\u4e8b\u4ef6\u3002<\/code><code>const emit = defineEmits(['changeMsg'])<\/code><code><br><\/code><code>function handleClick() {<\/code><code>  \/\/ \u53c2\u65701\uff1a\u4e8b\u4ef6\u540d<\/code><code>  \/\/ \u53c2\u65702\uff1a\u4f20\u7ed9\u7236\u7ec4\u4ef6\u7684\u503c<\/code><code>  emit('changeMsg', '\u9ca8\u9c7c\u8fa3\u6912')<\/code><code>}<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u548c <code>props<\/code> \u4e00\u6837\uff0c\u5728 <code>&lt;script setup&gt;<\/code> \u4e2d\u5fc5\u987b\u4f7f\u7528 <code>defineEmits<\/code> API \u6765\u58f0\u660e <code>emits<\/code>\uff0c\u5b83\u5177\u5907\u5b8c\u6574\u7684\u63a8\u65ad\u5e76\u4e14\u5728 <code>&lt;script setup&gt;<\/code> \u4e2d\u662f\u76f4\u63a5\u53ef\u7528\u7684\u3002\u66f4\u591a\u7ec6\u8282\u8bf7\u770b <a href=\"https:\/\/v3.cn.vuejs.org\/api\/sfc-script-setup.html#defineprops-%E5%92%8C-defineemits\">\u6587\u6863<\/a>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>&lt;script setup&gt;<\/code> \u4e2d\uff0c<code>defineEmits<\/code> \u4e0d\u9700\u8981\u53e6\u5916\u5f15\u5165\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">expose \/ ref<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u7ec4\u4ef6\u53ef\u4ee5\u901a\u8fc7 <code>expose<\/code> \u66b4\u9732\u81ea\u8eab\u7684\u65b9\u6cd5\u548c\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7236\u7ec4\u4ef6\u901a\u8fc7 <code>ref<\/code> \u83b7\u53d6\u5230\u5b50\u7ec4\u4ef6\u5e76\u8c03\u7528\u5176\u65b9\u6cd5\u6216\u8bbf\u95ee\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/api\/options-data.html#expose\">expose \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528\u4f8b\u5b50\u8bf4\u8bdd<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/72\/72fa93d7de0a03aaa2a664fd664ddf3e.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;\u7236\u7ec4\u4ef6\uff1a\u62ff\u5230\u5b50\u7ec4\u4ef6\u7684message\u6570\u636e\uff1a{{ msg }}&lt;\/div&gt;<\/code><code>  &lt;button @click=\"callChildFn\"&gt;\u8c03\u7528\u5b50\u7ec4\u4ef6\u7684\u65b9\u6cd5&lt;\/button&gt;<\/code><code><br><\/code><code>  &lt;hr&gt;<\/code><code><br><\/code><code>  &lt;Child ref=\"com\" \/&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref, onMounted } from 'vue'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code><br><\/code><code>const com = ref(null) \/\/ \u901a\u8fc7 \u6a21\u677fref \u7ed1\u5b9a\u5b50\u7ec4\u4ef6<\/code><code><br><\/code><code>const msg = ref('')<\/code><code><br><\/code><code>onMounted(() =&gt; {<\/code><code>  \/\/ \u5728\u52a0\u8f7d\u5b8c\u6210\u540e\uff0c\u5c06\u5b50\u7ec4\u4ef6\u7684 message \u8d4b\u503c\u7ed9 msg<\/code><code>  msg.value = com.value.message<\/code><code>})<\/code><code><br><\/code><code>function callChildFn() {<\/code><code>  \/\/ \u8c03\u7528\u5b50\u7ec4\u4ef6\u7684 changeMessage \u65b9\u6cd5<\/code><code>  com.value.changeMessage('\u849c\u5934\u738b\u516b')<\/code><code><br><\/code><code>  \/\/ \u91cd\u65b0\u5c06 \u5b50\u7ec4\u4ef6\u7684message \u8d4b\u503c\u7ed9 msg<\/code><code>  msg.value = com.value.message<\/code><code>}<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;\u5b50\u7ec4\u4ef6\uff1a{{ message }}&lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code><br><\/code><code>const message = ref('\u87d1\u8782\u6076\u9738')<\/code><code><br><\/code><code>function changeMessage(data) {<\/code><code>  message.value = data<\/code><code>}<\/code><code><br><\/code><code>\u4f7f\u7528 defineExpose \u5411\u5916\u66b4\u9732\u6307\u5b9a\u7684\u6570\u636e\u548c\u65b9\u6cd5<\/code><code>defineExpose({<\/code><code>  message,<\/code><code>  changeMessage<\/code><code>})<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>&lt;script setup&gt;<\/code> \u4e2d\uff0c<code>defineExpose<\/code> \u4e0d\u9700\u8981\u53e6\u5916\u5f15\u5165\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/api\/options-data.html#expose\">expose \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/api\/sfc-script-setup.html#defineexpose\">defineExpose \u6587\u6863<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Non-Props<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u8c13\u7684 <code>Non-Props<\/code> \u5c31\u662f <a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-attrs.html\">\u975e Prop \u7684 Attribute<\/a>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u610f\u601d\u662f\u5728\u5b50\u7ec4\u4ef6\u4e2d\uff0c\u6ca1\u4f7f\u7528 <code>prop<\/code> \u6216 <code>emits<\/code> \u5b9a\u4e49\u7684 <code>attribute<\/code>\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>$attrs<\/code> \u6765\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u89c1\u7684\u6709 <code>class<\/code> \u3001<code>style<\/code> \u548c <code>id<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-attrs.html\">\u975e Prop \u7684 Attribute \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u662f\u4e3e\u4e2a\u4f8b\u5b50\u4f1a\u76f4\u89c2\u70b9<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5355\u4e2a\u6839\u5143\u7d20\u7684\u60c5\u51b5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;Child msg=\"\u96f7\u7334 \u4e16\u754c\uff01\" name=\"\u9ca8\u9c7c\u8fa3\u6912\" \/&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;\u5b50\u7ec4\u4ef6\uff1a\u6253\u5f00\u63a7\u5236\u53f0\u770b\u770b&lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/ce\/ce9329e1aa586cfdaf33d148730fdd89.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6253\u5f00\u63a7\u5236\u53f0\u53ef\u4ee5\u770b\u5230\uff0c\u5c5e\u6027\u88ab\u6302\u5230 <code>HTML<\/code> \u5143\u7d20\u4e0a\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u591a\u4e2a\u5143\u7d20\u7684\u60c5\u51b5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u5728 <code>Vue3<\/code> \u4e2d\uff0c\u7ec4\u4ef6\u5df2\u7ecf\u6ca1\u89c4\u5b9a\u53ea\u80fd\u6709\u4e00\u4e2a\u6839\u5143\u7d20\u4e86\u3002\u5982\u679c\u5b50\u7ec4\u4ef6\u662f\u591a\u4e2a\u5143\u7d20\u65f6\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u5c31\u4e0d\u751f\u6548\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;\u5b50\u7ec4\u4ef6\uff1a\u6253\u5f00\u63a7\u5236\u53f0\u770b\u770b&lt;\/div&gt;<\/code><code>  &lt;div&gt;\u5b50\u7ec4\u4ef6\uff1a\u6253\u5f00\u63a7\u5236\u53f0\u770b\u770b&lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/00\/004af0d3caa53ed92cfeb6e6c2423367.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u65f6\u53ef\u4ee5\u4f7f\u7528 <code>$attrs<\/code> \u7684\u65b9\u5f0f\u8fdb\u884c\u7ed1\u5b9a\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div :message=\"$attrs.msg\"&gt;\u53ea\u7ed1\u5b9a\u6307\u5b9a\u503c&lt;\/div&gt;<\/code><code>  &lt;div v-bind=\"$attrs\"&gt;\u5168\u7ed1\u5b9a&lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/03\/03a7947203c4632ab00137fd9ebc3c5a.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">v-model<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>v-model<\/code> \u662f <code>Vue<\/code> \u7684\u4e00\u4e2a\u8bed\u6cd5\u7cd6\u3002\u5728 <code>Vue3<\/code> \u4e2d\u7684\u73a9\u6cd5\u5c31\u66f4\u591a(\u6655)\u4e86\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5355\u503c\u7684\u60c5\u51b5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ec4\u4ef6\u4e0a\u7684 <code>v-model<\/code> \u4f7f\u7528 <code>modelValue<\/code> \u4f5c\u4e3a prop \u548c <code>update:modelValue<\/code> \u4f5c\u4e3a\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-custom-events.html#v-model-%E5%8F%82%E6%95%B0\">v-model \u53c2\u6570\u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;Child v-model=\"message\" \/&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code><br><\/code><code>const message = ref('\u96f7\u7334')<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div @click=\"handleClick\"&gt;{{modelValue}}&lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code><br><\/code><code>\/\/ \u63a5\u6536<\/code><code>const props = defineProps([<\/code><code>  'modelValue' \/\/ \u63a5\u6536\u7236\u7ec4\u4ef6\u4f7f\u7528 v-model \u4f20\u8fdb\u6765\u7684\u503c\uff0c\u5fc5\u987b\u7528 modelValue \u8fd9\u4e2a\u540d\u5b57\u6765\u63a5\u6536<\/code><code>])<\/code><code><br><\/code><code>const emit = defineEmits(['update:modelValue']) \/\/ \u5fc5\u987b\u7528 update:modelValue \u8fd9\u4e2a\u540d\u5b57\u6765\u901a\u77e5\u7236\u7ec4\u4ef6\u4fee\u6539\u503c<\/code><code><br><\/code><code>function handleClick() {<\/code><code>  \/\/ \u53c2\u65701\uff1a\u901a\u77e5\u7236\u7ec4\u4ef6\u4fee\u6539\u503c\u7684\u65b9\u6cd5\u540d<\/code><code>  \/\/ \u53c2\u65702\uff1a\u8981\u4fee\u6539\u7684\u503c<\/code><code>  emit('update:modelValue', '\u55b7\u5c04\u6cb3\u9a6c')<\/code><code>}<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/20\/204a31ae4b3c53c78d4bb794f23c3a0e.gif\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f60\u4e5f\u53ef\u4ee5\u8fd9\u6837\u5199\uff0c\u66f4\u52a0\u7b80\u5355<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div @click=\"$emit('update:modelValue', '\u55b7\u5c04\u6cb3\u9a6c')\"&gt;{{modelValue}}&lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code><br><\/code><code>\/\/ \u63a5\u6536<\/code><code>const props = defineProps([<\/code><code>  'modelValue' \/\/ \u63a5\u6536\u7236\u7ec4\u4ef6\u4f7f\u7528 v-model \u4f20\u8fdb\u6765\u7684\u503c\uff0c\u5fc5\u987b\u7528 modelValue \u8fd9\u4e2a\u540d\u5b57\u6765\u63a5\u6536<\/code><code>])<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u591a\u4e2a v-model \u7ed1\u5b9a<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-custom-events.html#%E5%A4%9A%E4%B8%AA-v-model-%E7%BB%91%E5%AE%9A\">\u591a\u4e2a v-model \u7ed1\u5b9a \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;Child v-model:msg1=\"message1\" v-model:msg2=\"message2\" \/&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code><br><\/code><code>const message1 = ref('\u96f7\u7334')<\/code><code><br><\/code><code>const message2 = ref('\u87d1\u8782\u6076\u9738')<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;&lt;button @click=\"changeMsg1\"&gt;\u4fee\u6539msg1&lt;\/button&gt; {{msg1}}&lt;\/div&gt;<\/code><code><br><\/code><code>  &lt;div&gt;&lt;button @click=\"changeMsg2\"&gt;\u4fee\u6539msg2&lt;\/button&gt; {{msg2}}&lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code><br><\/code><code>\/\/ \u63a5\u6536<\/code><code>const props = defineProps({<\/code><code>  msg1: String,<\/code><code>  msg2: String<\/code><code>})<\/code><code><br><\/code><code>const emit = defineEmits(['update:msg1', 'update:msg2'])<\/code><code><br><\/code><code>function changeMsg1() {<\/code><code>  emit('update:msg1', '\u9ca8\u9c7c\u8fa3\u6912')<\/code><code>}<\/code><code><br><\/code><code>function changeMsg2() {<\/code><code>  emit('update:msg2', '\u874e\u5b50\u83b1\u83b1')<\/code><code>}<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/2a\/2a421e302e66345eee9e659810f54573.gif\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">v-model \u4fee\u9970\u7b26<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>v-model<\/code> \u8fd8\u80fd\u901a\u8fc7 <code>.<\/code> \u7684\u65b9\u5f0f\u4f20\u5165\u4fee\u9970\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-custom-events.html#%E5%A4%84%E7%90%86-v-model-%E4%BF%AE%E9%A5%B0%E7%AC%A6\">v-model \u4fee\u9970\u7b26 \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;Child v-model.uppercase=\"message\" \/&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code><br><\/code><code>const message = ref('hello')<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;{{modelValue}}&lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref, onMounted } from 'vue'<\/code><code><br><\/code><code>const props = defineProps([<\/code><code>  'modelValue',<\/code><code>  'modelModifiers'<\/code><code>])<\/code><code><br><\/code><code>const emit = defineEmits(['update:modelValue'])<\/code><code><br><\/code><code>onMounted(() =&gt; {<\/code><code>  \/\/ \u5224\u65ad\u6709\u6ca1\u6709 uppercase \u4fee\u9970\u7b26\uff0c\u6709\u7684\u8bdd\u5c31\u6267\u884c toUpperCase() \u65b9\u6cd5<\/code><code>  if (props.modelModifiers.uppercase) {<\/code><code>    emit('update:modelValue', props.modelValue.toUpperCase())<\/code><code>  }<\/code><code>})<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/d2\/d27ffb089d748fb258fdffd1b1639280.png\" alt=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u63d2\u69fd slot<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u63d2\u69fd\u53ef\u4ee5\u7406\u89e3\u4e3a\u4f20\u4e00\u6bb5 <code>HTML<\/code> \u7247\u6bb5\u7ed9\u5b50\u7ec4\u4ef6\u3002\u5b50\u7ec4\u4ef6\u5c06 <code>&lt;slot&gt;<\/code> \u5143\u7d20\u4f5c\u4e3a\u627f\u8f7d\u5206\u53d1\u5185\u5bb9\u7684\u51fa\u53e3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-slots.html\">\u63d2\u69fd \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u6253\u7b97\u8bb2\u8bb2\u65e5\u5e38\u7528\u5f97\u6bd4\u8f83\u591a\u7684 3 \u79cd\u63d2\u69fd\uff1a\u9ed8\u8ba4\u63d2\u69fd\u3001\u5177\u540d\u63d2\u69fd\u3001\u4f5c\u7528\u57df\u63d2\u69fd\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9ed8\u8ba4\u63d2\u69fd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u63d2\u69fd\u7684\u57fa\u7840\u7528\u6cd5\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728 <strong>\u5b50\u7ec4\u4ef6<\/strong> \u4e2d\u4f7f\u7528 <code>&lt;slot&gt;<\/code> \u6807\u7b7e\uff0c\u5c31\u4f1a\u5c06\u7236\u7ec4\u4ef6\u4f20\u8fdb\u6765\u7684 <code>HTML<\/code> \u5185\u5bb9\u6e32\u67d3\u51fa\u6765\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-slots.html#%E6%8F%92%E6%A7%BD%E5%86%85%E5%AE%B9\">\u9ed8\u8ba4\u63d2\u69fd \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;Child&gt;<\/code><code>    &lt;div&gt;\u96f7\u7334\u554a&lt;\/div&gt;<\/code><code>  &lt;\/Child&gt;<\/code><code>&lt;\/template&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    &lt;slot&gt;&lt;\/slot&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;br&gt;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5177\u540d\u63d2\u69fd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5177\u540d\u63d2\u69fd<\/strong> \u5c31\u662f\u5728 <strong>\u9ed8\u8ba4\u63d2\u69fd<\/strong> \u7684\u57fa\u7840\u4e0a\u8fdb\u884c\u5206\u7c7b\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a\u5bf9\u53f7\u5165\u5ea7\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-slots.html#%E5%85%B7%E5%90%8D%E6%8F%92%E6%A7%BD\">\u5177\u540d\u63d2\u69fd \u6587\u6863<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/36\/360fc07853d3f005f0d499346440876c.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;Child&gt;<\/code><code>    &lt;template v-slot:monkey&gt;<\/code><code>      &lt;div&gt;\u96f7\u7334\u554a&lt;\/div&gt;<\/code><code>    &lt;\/template&gt;<\/code><code><br><\/code><code>    &lt;button&gt;\u9ca8\u9c7c\u8fa3\u6912&lt;\/button&gt;<\/code><code>  &lt;\/Child&gt;<\/code><code>&lt;\/template&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    &lt;!-- \u9ed8\u8ba4\u63d2\u69fd --&gt;<\/code><code>    &lt;slot&gt;&lt;\/slot&gt;<\/code><code>    &lt;!-- \u5177\u540d\u63d2\u69fd --&gt;<\/code><code>    &lt;slot name=\"monkey\"&gt;&lt;\/slot&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7236\u7ec4\u4ef6\u9700\u8981\u4f7f\u7528 <code>&lt;template&gt;<\/code> \u6807\u7b7e\uff0c\u5e76\u5728\u6807\u7b7e\u4e0a\u4f7f\u7528 <code>v-solt: + \u540d\u79f0<\/code> \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b50\u7ec4\u4ef6\u9700\u8981\u5728 <code>&lt;slot&gt;<\/code> \u6807\u7b7e\u91cc\u7528 <code>name= \u540d\u79f0<\/code> \u5bf9\u5e94\u63a5\u6536\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u5c31\u662f <strong>\u5bf9\u53f7\u5165\u5ea7<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u540e\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u63d2\u69fd\u5185\u5bb9\u7684\u6392\u7248\u987a\u5e8f\uff0c\u662f <strong>\u4ee5\u5b50\u7ec4\u4ef6\u91cc\u7684\u6392\u7248\u4e3a\u51c6<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u8fd9\u4e2a\u4f8b\u5b50\u5c31\u662f\u8fd9\u6837\uff0c\u4f60\u53ef\u4ee5\u4ed4\u7ec6\u89c2\u5bdf\u5b50\u7ec4\u4ef6\u4f20\u5165\u987a\u5e8f\u548c\u5b50\u7ec4\u4ef6\u7684\u6392\u7248\u987a\u5e8f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f5c\u7528\u57df\u63d2\u69fd<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u7528\u8fc7 <code>Element-Plus<\/code> \u8fd9\u7c7b UI \u6846\u67b6 \u7684 <code>Table<\/code> \uff0c\u5e94\u8be5\u5c31\u80fd\u5f88\u597d\u7684\u7406\u89e3\u4ec0\u4e48\u53eb\u4f5c\u7528\u57df\u63d2\u69fd\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-slots.html#%E4%BD%9C%E7%94%A8%E5%9F%9F%E6%8F%92%E6%A7%BD\">\u4f5c\u7528\u57df\u63d2\u69fd \u6587\u6863<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/61\/6133beb78b8dd1d0d85291b77856218a.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;!-- v-slot=\"{scope}\" \u83b7\u53d6\u5b50\u7ec4\u4ef6\u4f20\u4e0a\u6765\u7684\u6570\u636e --&gt;<\/code><code>  &lt;!-- :list=\"list\" \u628alist\u4f20\u7ed9\u5b50\u7ec4\u4ef6 --&gt;<\/code><code>  &lt;Child v-slot=\"{scope}\" :list=\"list\"&gt;<\/code><code>    &lt;div&gt;<\/code><code>      &lt;div&gt;\u540d\u5b57\uff1a{{ scope.name }}&lt;\/div&gt;<\/code><code>      &lt;div&gt;\u804c\u4e1a\uff1a{{ scope.occupation }}&lt;\/div&gt;<\/code><code>      &lt;hr&gt;<\/code><code>    &lt;\/div&gt;<\/code><code>  &lt;\/Child&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code><br><\/code><code>const list = ref([<\/code><code>  { name: '\u96f7\u7334', occupation: '\u6253\u96f7'},<\/code><code>  { name: '\u9ca8\u9c7c\u8fa3\u6912', occupation: '\u6e38\u6cf3'},<\/code><code>  { name: '\u87d1\u8782\u6076\u9738', occupation: '\u626b\u5730'},<\/code><code>])<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    &lt;!-- \u7528 :scope=\"item\" \u8fd4\u56de\u6bcf\u4e00\u9879 --&gt;<\/code><code>    &lt;slot v-for=\"item in list\" :scope=\"item\" \/&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>const props = defineProps({<\/code><code>  list: {<\/code><code>    type: Array,<\/code><code>    default: () =&gt; []<\/code><code>  }<\/code><code>})<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u6ca1\u5199\u6837\u5f0f\uff0c\u6240\u4ee5\u7528 <code>hr<\/code> \u5143\u7d20\u8ba9\u89c6\u89c9\u4e0a\u770b\u4e0a\u53bb\u6bd4\u8f83\u6e05\u6670 <s>\u6211\u5c31\u662f\u61d2<\/s>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">provide \/ inject<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u9047\u5230\u591a\u5c42\u4f20\u503c\u65f6\uff0c\u4f7f\u7528 <code>props<\/code> \u548c <code>emit<\/code> \u7684\u65b9\u5f0f\u4f1a\u663e\u5f97\u6bd4\u8f83\u7b28\u62d9\u3002\u8fd9\u65f6\u5c31\u53ef\u4ee5\u7528 <code>provide<\/code> \u548c <code>inject<\/code> \u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>provide<\/code> \u662f\u5728\u7236\u7ec4\u4ef6\u91cc\u4f7f\u7528\u7684\uff0c\u53ef\u4ee5\u5f80\u4e0b\u4f20\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>inject<\/code> \u662f\u5728\u5b50(\u540e\u4ee3)\u7ec4\u4ef6\u91cc\u4f7f\u7528\u7684\uff0c\u53ef\u4ee5\u7f51\u4e0a\u53d6\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u65e0\u8bba\u7ec4\u4ef6\u5c42\u6b21\u7ed3\u6784\u6709\u591a\u6df1\uff0c\u7236\u7ec4\u4ef6\u90fd\u53ef\u4ee5\u4f5c\u4e3a\u5176\u6240\u6709\u5b50\u7ec4\u4ef6\u7684\u4f9d\u8d56\u63d0\u4f9b\u8005\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/guide\/component-provide-inject.html\">provide \/ inject \u6587\u6863<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/static001.geekbang.org\/infoq\/76\/760cfa5c8d123cd1755e928d9853a344.png\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;Child&gt;&lt;\/Child&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref, provide, readonly } from 'vue'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code><br><\/code><code>const name = ref('\u731b\u864e\u4e0b\u5c71')<\/code><code>const msg = ref('\u96f7\u7334')<\/code><code><br><\/code><code>\/\/ \u4f7f\u7528readonly\u53ef\u4ee5\u8ba9\u5b50\u7ec4\u4ef6\u65e0\u6cd5\u76f4\u63a5\u4fee\u6539\uff0c\u9700\u8981\u8c03\u7528provide\u5f80\u4e0b\u4f20\u7684\u65b9\u6cd5\u6765\u4fee\u6539<\/code><code>provide('name', readonly(name))<\/code><code><br><\/code><code>provide('msg', msg)<\/code><code><br><\/code><code>provide('changeName', (value) =&gt; {<\/code><code>  name.value = value<\/code><code>})<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    &lt;div&gt;msg: {{ msg }}&lt;\/div&gt;<\/code><code>    &lt;div&gt;name: {{name}}&lt;\/div&gt;<\/code><code>    &lt;button @click=\"handleClick\"&gt;\u4fee\u6539&lt;\/button&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { inject } from 'vue'<\/code><code><br><\/code><code>const name = inject('name', 'hello') \/\/ \u770b\u770b\u6709\u6ca1\u6709\u503c\uff0c\u6ca1\u503c\u7684\u8bdd\u5c31\u9002\u7528\u9ed8\u8ba4\u503c\uff08\u8fd9\u91cc\u9ed8\u8ba4\u503c\u662fhello\uff09<\/code><code>const msg = inject('msg')<\/code><code>const changeName = inject('changeName')<\/code><code><br><\/code><code>function handleClick() {<\/code><code>  \/\/ \u8fd9\u6837\u5199\u4e0d\u5408\u9002\uff0c\u56e0\u4e3avue\u91cc\u63a8\u8350\u4f7f\u7528\u5355\u5411\u6570\u636e\u6d41\uff0c\u5f53\u7236\u7ea7\u4f7f\u7528readonly\u540e\uff0c\u8fd9\u884c\u4ee3\u7801\u662f\u4e0d\u4f1a\u751f\u6548\u7684\u3002\u6ca1\u4f7f\u7528\u4e4b\u524d\u624d\u4f1a\u751f\u6548\u3002<\/code><code>  \/\/ name.value = '\u96f7\u7334'<\/code><code><br><\/code><code>  \/\/ \u6b63\u786e\u7684\u65b9\u5f0f<\/code><code>  changeName('\u864e\u8eaf\u4e00\u9707')<\/code><code><br><\/code><code>  \/\/ \u56e0\u4e3a msg \u6ca1\u88ab readonly \u8fc7\uff0c\u6240\u4ee5\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u503c<\/code><code>  msg.value = '\u4e16\u754c'<\/code><code>}<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><code>provide<\/code> \u53ef\u4ee5\u914d\u5408 <code>readonly<\/code> \u4e00\u8d77\u4f7f\u7528\uff0c\u8be6\u60c5\u53ef\u4ee5\u770b\u4e0a\u9762\u4f8b\u5b50\u548c\u6ce8\u91ca\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>provide<\/code> \u548c <code>inject<\/code> \u5176\u5b9e\u4e3b\u8981\u662f\u7528\u5728\u6df1\u5c42\u5173\u7cfb\u4e2d\u4f20\u503c\uff0c\u4e0a\u9762\u7684\u4f8b\u5b50\u53ea\u6709\u7236\u5b50 2 \u5c42\uff0c\u53ea\u662f\u4e3a\u4e86\u4e3e\u4f8b\u8bf4\u660e <s>\u6211\u61d2<\/s>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ebf bus<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>Vue2<\/code> \u6709\u603b\u7ebf\u4f20\u503c\u7684\u65b9\u6cd5\uff0c\u6211\u4eec\u5728 <code>Vue3<\/code> \u4e2d\u4e5f\u53ef\u4ee5\u81ea\u5df1\u6a21\u62df\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u65b9\u5f0f\u5176\u5b9e\u6709\u70b9\u50cf <code>Vuex<\/code> \u6216\u8005 <code>Pinia<\/code> \u90a3\u6837\uff0c\u5f04\u4e00\u4e2a\u72ec\u7acb\u7684\u5de5\u5177\u51fa\u6765\u4e13\u95e8\u63a7\u5236\u6570\u636e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u548c <code>Vuex<\/code> \u6216 <code>Pinia<\/code> \u76f8\u6bd4\uff0c\u6211\u4eec\u81ea\u5df1\u5199\u7684\u8fd9\u4e2a\u65b9\u6cd5\u5e76\u6ca1\u6709\u5f88\u597d\u7684\u6570\u636e\u8ddf\u8e2a\u4e4b\u7c7b\u7684\u7279\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u539f\u7406<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u521b\u5efa\u4e00\u4e2a <code>Bus.js<\/code> \u6587\u4ef6\uff0c\u7528\u6765\u63a7\u5236\u6570\u636e\u548c\u6ce8\u518c\u4e8b\u4ef6\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Bus.js<\/code> \u91cc\u6709\u4e00\u4e2a <code>Bus<\/code> \u7c7b<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>eventList<\/code> \u662f\u5fc5\u987b\u9879\uff0c\u7528\u6765\u5b58\u653e\u4e8b\u4ef6\u5217\u8868\u7684\u3002<\/li><li><code>constructor<\/code> \u91cc\u9664\u4e86 <code>eventList<\/code> \u5916\uff0c\u5176\u4ed6\u90fd\u662f\u81ea\u5b9a\u4e49\u6570\u636e\uff0c\u516c\u5171\u6570\u636e\u5c31\u662f\u5b58\u5728\u8fd9\u91cc\u7684\u3002<\/li><li><code>$on<\/code> \u65b9\u6cd5\u7528\u6765\u6ce8\u518c\u4e8b\u4ef6\u3002<\/li><li><code>$emit<\/code> \u65b9\u6cd5\u53ef\u4ee5\u8c03\u7528 <code>$on<\/code> \u91cc\u7684\u4e8b\u4ef6\u3002<\/li><li><code>$off<\/code> \u65b9\u6cd5\u53ef\u4ee5\u6ce8\u9500 <code>eventList<\/code> \u91cc\u7684\u4e8b\u4ef6\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u9700\u8981\u7528\u5230\u603b\u7ebf\u7684\u7ec4\u4ef6\uff0c\u90fd\u5bfc\u5165 <code>Bus.js<\/code> \uff0c\u5c31\u53ef\u4ee5\u5171\u540c\u64cd\u4f5c\u4e00\u4efd\u6570\u636e\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Bus.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import { ref } from 'vue'<\/code><code><br><\/code><code>class Bus {<\/code><code>  constructor() {<\/code><code>    \/\/ \u6536\u96c6\u8ba2\u9605\u4fe1\u606f,\u8c03\u5ea6\u4e2d\u5fc3<\/code><code>  this.eventList = {}, \/\/ \u4e8b\u4ef6\u5217\u8868\uff0c\u8fd9\u9879\u662f\u5fc5\u987b\u7684<\/code><code>    \/\/ \u4e0b\u9762\u7684\u90fd\u662f\u81ea\u5b9a\u4e49\u503c<\/code><code>  this.msg = ref('\u8fd9\u662f\u4e00\u6761\u603b\u7ebf\u7684\u4fe1\u606f')<\/code><code>  }<\/code><code><br><\/code><code>  \/\/ \u8ba2\u9605<\/code><code>  $on(name, fn) {<\/code><code>  this.eventList[name] = this.eventList[name] || []<\/code><code>  this.eventList[name].push(fn)<\/code><code>  }<\/code><code><br><\/code><code>  \/\/ \u53d1\u5e03<\/code><code>  $emit(name, data) {<\/code><code>  if (this.eventList[name]) {<\/code><code>      this.eventList[name].forEach((fn) =&gt; {<\/code><code>        fn(data)<\/code><code>      });<\/code><code>  }<\/code><code>  }<\/code><code><br><\/code><code>  \/\/ \u53d6\u6d88\u8ba2\u9605<\/code><code>  $off(name) {<\/code><code>      if (this.eventList[name]) {<\/code><code>    delete this.eventList[name]<\/code><code>  }<\/code><code>  }<\/code><code>}<\/code><code><br><\/code><code>export default new Bus()<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    \u7236\u7ec4\u4ef6: <\/code><code>    &lt;span style=\"margin-right: 30px;\"&gt;message: {{ message }}&lt;\/span&gt;<\/code><code>    &lt;span&gt;msg: {{ msg }}&lt;\/span&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>  &lt;Child&gt;&lt;\/Child&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref } from 'vue'<\/code><code>import Bus from '.\/Bus.js'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code><br><\/code><code>const msg = ref(Bus.msg)<\/code><code><br><\/code><code>const message = ref('hello')<\/code><code><br><\/code><code>\/\/ \u7528\u76d1\u542c\u7684\u5199\u6cd5<\/code><code>Bus.$on('changeMsg', data =&gt; {<\/code><code>  message.value = data<\/code><code>})<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    \u5b50\u7ec4\u4ef6\uff1a<\/code><code>    &lt;button @click=\"handleBusEmit\"&gt;\u89e6\u53d1Bus.$emit&lt;\/button&gt;<\/code><code>    &lt;button @click=\"changeBusMsg\"&gt;\u4fee\u6539\u603b\u7ebf\u91cc\u7684 msg&lt;\/button&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import Bus from '..\/Bus.js'<\/code><code><br><\/code><code>function handleBusEmit() {<\/code><code>  Bus.$emit('changeMsg', '\u96f7\u7334\u554a')<\/code><code>}<\/code><code><br><\/code><code>function changeBusMsg() {<\/code><code>  \/\/ console.log(Bus.msg)<\/code><code>  Bus.msg.value = '\u5728\u5b50\u7ec4\u4ef6\u91cc\u4fee\u6539\u4e86\u603b\u7ebf\u7684\u503c'<\/code><code>}<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u65b9\u6cd5\u5176\u5b9e\u8fd8\u633a\u597d\u7528\u7684\uff0c\u4f46\u5149\u770b\u53ef\u80fd\u6709\u70b9\u61f5\uff0c\u8bf7\u5927\u5bb6\u52a1\u5fc5\u4eb2\u624b\u6572\u4e00\u4e0b\u4ee3\u7801\u5b9e\u8df5\u4e00\u4e0b\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">getCurrentInstance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>getcurrentinstance<\/code> \u662f <code>vue<\/code> \u63d0\u4f9b\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u652f\u6301\u8bbf\u95ee\u5185\u90e8\u7ec4\u4ef6\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><code>getCurrentInstance<\/code> \u53ea\u66b4\u9732\u7ed9\u9ad8\u9636\u4f7f\u7528\u573a\u666f\uff0c\u5178\u578b\u7684\u6bd4\u5982\u5728\u5e93\u4e2d\u3002\u5f3a\u70c8\u53cd\u5bf9\u5728\u5e94\u7528\u7684\u4ee3\u7801\u4e2d\u4f7f\u7528 <code>getCurrentInstance<\/code>\u3002\u8bf7<strong>\u4e0d\u8981<\/strong>\u628a\u5b83\u5f53\u4f5c\u5728\u7ec4\u5408\u5f0f API \u4e2d\u83b7\u53d6 <code>this<\/code> \u7684\u66ff\u4ee3\u65b9\u6848\u6765\u4f7f\u7528\u3002<\/p><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u767d\u4e86\uff0c\u8fd9\u4e2a\u65b9\u6cd5 <strong>\u9002\u5408\u5728\u5f00\u53d1\u7ec4\u4ef6\u5e93\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528<\/strong>\uff0c\u4e0d\u9002\u5408\u65e5\u5e38\u4e1a\u52a1\u5f00\u53d1\u4e2d\u4f7f\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code><strong>getCurrentInstance<\/strong><\/code><strong> \u53ea\u80fd\u5728 <\/strong><a href=\"https:\/\/v3.cn.vuejs.org\/api\/composition-api.html#setup\"><strong>setup<\/strong><\/a><strong> \u6216<\/strong><a href=\"https:\/\/v3.cn.vuejs.org\/api\/composition-api.html#%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F%E9%92%A9%E5%AD%90\"><strong>\u751f\u547d\u5468\u671f\u94a9\u5b50<\/strong><\/a><strong>\u4e2d\u8c03\u7528\u3002<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/v3.cn.vuejs.org\/api\/composition-api.html#getcurrentinstance\">getcurrentinstance \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>&lt;script setup&gt;<\/code> \u4e2d\uff0c\u6211\u6a21\u62df\u4e86\u7c7b\u4f3c <code>$parent<\/code> \u548c <code>$children<\/code> \u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7236\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;\u7236\u7ec4\u4ef6 message \u7684\u503c: {{ message }}&lt;\/div&gt;<\/code><code>  &lt;button @click=\"handleClick\"&gt;\u83b7\u53d6\u5b50\u7ec4\u4ef6&lt;\/button&gt;<\/code><code>  &lt;Child&gt;&lt;\/Child&gt;<\/code><code>  &lt;Child&gt;&lt;\/Child&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { ref, getCurrentInstance, onMounted } from 'vue'<\/code><code>import Child from '.\/components\/Child.vue'<\/code><code><br><\/code><code>const message = ref('\u96f7\u7334\u554a')<\/code><code><br><\/code><code>let instance = null<\/code><code><br><\/code><code>onMounted(() =&gt; {<\/code><code>  instance = getCurrentInstance()<\/code><code>})<\/code><code><br><\/code><code>\/\/ \u5b50\u7ec4\u4ef6\u5217\u8868<\/code><code>let childrenList = []<\/code><code><br><\/code><code>\/\/ \u6ce8\u518c\u7ec4\u4ef6<\/code><code>function registrationCom(com) {<\/code><code>  childrenList.push(com)<\/code><code>}<\/code><code><br><\/code><code>function handleClick() {<\/code><code>  if (childrenList.length &gt; 0) {<\/code><code>    childrenList.forEach(item =&gt; {<\/code><code>      console.log('\u7ec4\u4ef6\u5b9e\u4f8b\uff1a', item)<\/code><code>      console.log('\u7ec4\u4ef6\u540d(name)\uff1a', item.type.name)<\/code><code>      console.log('\u7ec4\u4ef6\u8f93\u5165\u6846\u7684\u503c\uff1a', item.devtoolsRawSetupState.inputValue)<\/code><code>      console.log('---------------------------------------')<\/code><code>    })<\/code><code>  }<\/code><code>}<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5b50\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    &lt;div&gt;----------------------------&lt;\/div&gt;<\/code><code>    \u5b50\u7ec4\u4ef6\uff1a&lt;button @click=\"handleClick\"&gt;\u83b7\u53d6\u7236\u7ec4\u4ef6\u7684\u503c&lt;\/button&gt;<\/code><code>    &lt;br&gt;<\/code><code>    &lt;input type=\"text\" v-model=\"inputValue\"&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script&gt;<\/code><code>export default {<\/code><code>  name: 'ccccc'<\/code><code>}<\/code><code>&lt;\/script&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { getCurrentInstance, onMounted, nextTick, ref } from 'vue'<\/code><code><br><\/code><code>const inputValue = ref('')<\/code><code><br><\/code><code>let instance = null<\/code><code><br><\/code><code>onMounted(() =&gt; {<\/code><code>  instance = getCurrentInstance()<\/code><code>  nextTick(() =&gt; {<\/code><code>    instance.parent.devtoolsRawSetupState.registrationCom(instance)<\/code><code>  })<\/code><code><br><\/code><code>})<\/code><code><br><\/code><code>function handleClick() {<\/code><code>  let msg = instance.parent.devtoolsRawSetupState.message<\/code><code>  msg.value = '\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8'<\/code><code>}<\/code><code><br><\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53ef\u4ee5\u5c06\u4ee3\u7801\u590d\u5236\u5230\u4f60\u7684\u9879\u76ee\u4e2d\u8fd0\u884c\u8bd5\u8bd5\u770b\uff0c\u6700\u597d\u8fd8\u662f\u6572\u4e00\u904d\u54af\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Vuex<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Vuex<\/code> \u4e3b\u8981\u89e3\u51b3 <strong>\u8de8\u7ec4\u4ef6\u901a\u4fe1<\/strong> \u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>Vue3<\/code> \u4e2d\uff0c\u9700\u8981\u4f7f\u7528 <code>Vuex v4.x<\/code> \u7248\u672c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b89\u88c5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528 <code>npm<\/code> \u6216\u8005 <code>Yarn<\/code> \u5b89\u88c5\u5230\u9879\u76ee\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>npm install vuex@next --save<\/code><code><br><\/code><code># \u6216<\/code><code><br><\/code><code>yarn add vuex@next --save<\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b89\u88c5\u6210\u529f\u540e\uff0c\u5728 <code>src<\/code> \u76ee\u5f55\u4e0b\u521b\u5efa <code>store<\/code> \u76ee\u5f55\uff0c\u518d\u5728 <code>store<\/code> \u4e0b\u521b\u5efa <code>index.js<\/code> \u6587\u4ef6\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ store\/index.js<\/code><code><br><\/code><code>import { createStore } from 'vuex'<\/code><code><br><\/code><code>export default createStore({<\/code><code>  state: {<\/code><code>  },<\/code><code>  getters: {<\/code><code>  },<\/code><code>  mutations: {<\/code><code>  },<\/code><code>  actions: {<\/code><code>  },<\/code><code>  modules: {<\/code><code>  }<\/code><code>})<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>store\/index.js<\/code> \u4e0b\u8f93\u5165\u4ee5\u4e0a\u5185\u5bb9\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>state<\/code>\uff1a\u6570\u636e\u4ed3\u5e93\uff0c\u7528\u6765\u5b58\u6570\u636e\u7684\u3002<\/li><li><code>getters<\/code>\uff1a\u83b7\u53d6\u6570\u636e\u7684\uff0c\u6709\u70b9\u50cf <code>computed<\/code> \u7684\u7528\u6cd5(\u4e2a\u4eba\u89c9\u5f97)\u3002<\/li><li><code>mutations<\/code>: \u66f4\u6539 <code>state<\/code> \u6570\u636e\u7684\u65b9\u6cd5\u90fd\u8981\u5199\u5728 <code>mutations<\/code> \u91cc\u3002<\/li><li><code>actions<\/code>\uff1a\u5f02\u6b65\u5f02\u6b65\u5f02\u6b65\uff0c\u5f02\u6b65\u7684\u65b9\u6cd5\u90fd\u5199\u5728\u8fd9\u91cc\uff0c\u4f46\u6700\u540e\u8fd8\u662f\u9700\u8981\u901a\u8fc7 <code>mutations<\/code> \u6765\u4fee\u6539 <code>state<\/code> \u7684\u6570\u636e\u3002<\/li><li><code>modules<\/code>\uff1a\u5206\u5305\u3002\u5982\u679c\u9879\u76ee\u6bd4\u8f83\u5927\uff0c\u53ef\u4ee5\u5c06\u4e1a\u52a1\u62c6\u6563\u6210\u72ec\u7acb\u6a21\u5757\uff0c\u7136\u540e\u5206\u6587\u4ef6\u7ba1\u7406\u548c\u5b58\u653e\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5728 <code>src\/main.js<\/code> \u4e2d\u5f15\u5165<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import { createApp } from 'vue'<\/code><code>import App from '.\/App.vue'<\/code><code>import store from '.\/store'<\/code><code><br><\/code><code>const app = createApp(App)<\/code><code><br><\/code><code>app<\/code><code>  .use(store)<\/code><code>  .mount('#app')<\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">State<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>store\/index.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ store\/index.js<\/code><code><br><\/code><code>import { createStore } from 'vuex'<\/code><code><br><\/code><code>export default createStore({<\/code><code>  state: {<\/code><code>    msg: '\u96f7\u7334'<\/code><code>  }<\/code><code>})<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ xxx.vue<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { useStore } from 'vuex'<\/code><code><br><\/code><code>const store = useStore()<\/code><code><br><\/code><code>console.log(store.state.msg) \/\/ \u96f7\u7334<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Getter<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u89c9\u5f97 <code>Getter<\/code> \u65b9\u6cd5\u548c <code>computed<\/code> \u662f\u6709\u70b9\u50cf\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982\u6211\u4eec\u9700\u8981\u8fc7\u6ee4\u4e00\u4e0b\u6570\u636e\uff0c\u6216\u8005\u8fd4\u56de\u65f6\u7ec4\u88c5\u4e00\u4e0b\u6570\u636e\uff0c\u90fd\u53ef\u4ee5\u7528 <code>Getter<\/code> \u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>store\/index.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ store\/index.js<\/code><code><br><\/code><code>import { createStore } from 'vuex'<\/code><code><br><\/code><code>export default createStore({<\/code><code>  state: {<\/code><code>    msg: '\u96f7\u7334'<\/code><code>  },<\/code><code>  getters: {<\/code><code>    getMsg(state) {<\/code><code>      return state.msg + ' \u4e16\u754c\uff01'<\/code><code>    }<\/code><code>  }<\/code><code>})<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ xxx.vue<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { useStore } from 'vuex'<\/code><code><br><\/code><code>const store = useStore()<\/code><code><br><\/code><code>console.log(store.getters.getMsg) \/\/ \u96f7\u7334 \u4e16\u754c\uff01<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Mutation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Mutation<\/code> \u662f\u4fee\u6539 <code>State<\/code> \u6570\u636e\u7684\u552f\u4e00\u65b9\u6cd5\uff0c\u8fd9\u6837 <code>Vuex<\/code> \u624d\u53ef\u4ee5\u8ddf\u8e2a\u6570\u636e\u6d41\u5411\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7ec4\u4ef6\u4e2d\u901a\u8fc7 <code>commit<\/code> \u8c03\u7528\u5373\u53ef\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>store\/index.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ store\/index.js<\/code><code><br><\/code><code>import { createStore } from 'vuex'<\/code><code><br><\/code><code>export default createStore({<\/code><code>  state: {<\/code><code>    msg: '\u96f7\u7334'<\/code><code>  },<\/code><code>  mutations: {<\/code><code>    changeMsg(state, data) {<\/code><code>      state.msg = data<\/code><code>    }<\/code><code>  }<\/code><code>})<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ xxx.vue<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { useStore } from 'vuex'<\/code><code><br><\/code><code>const store = useStore()<\/code><code><br><\/code><code>store.commit('changeMsg', '\u849c\u5934\u738b\u516b')<\/code><code><br><\/code><code>console.log(store.state.msg) \/\/ \u849c\u5934\u738b\u516b<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Action<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4e60\u60ef\u5c06\u5f02\u6b65\u7684\u4e1c\u897f\u653e\u5728 <code>Action<\/code> \u65b9\u6cd5\u91cc\u5199\uff0c\u7136\u540e\u5728\u7ec4\u4ef6\u4f7f\u7528 <code>dispatch<\/code> \u65b9\u6cd5\u8c03\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>store\/index.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ store\/index.js<\/code><code><br><\/code><code>import { createStore } from 'vuex'<\/code><code><br><\/code><code>export default createStore({<\/code><code>  state: {<\/code><code>    msg: '\u96f7\u7334'<\/code><code>  },<\/code><code>  mutations: {<\/code><code>    changeMsg(state, data) {<\/code><code>      state.msg = data<\/code><code>    }<\/code><code>  },<\/code><code>  actions: {<\/code><code>    fetchMsg(context) {<\/code><code>      \/\/ \u6a21\u62dfajax\u8bf7\u6c42<\/code><code>      setTimeout(() =&gt; {<\/code><code>        context.commit('changeMsg', '\u9ca8\u9c7c\u8fa3\u6912')<\/code><code>      }, 1000)<\/code><code>    }<\/code><code>  }<\/code><code>})<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ xxx.vue<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { useStore } from 'vuex'<\/code><code><br><\/code><code>const store = useStore()<\/code><code><br><\/code><code>store.dispatch('fetchMsg')<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Module<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Module<\/code> \u5c31\u662f\u4f20\u8bf4\u4e2d\u7684\u5206\u5305\u4e86\u3002\u8fd9\u9700\u8981\u4f60\u5c06\u4e0d\u540c\u6a21\u5757\u7684\u6570\u636e\u62c6\u5206\u6210\u4e00\u4e2a\u4e2a <code>js<\/code> \u6587\u4ef6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u76ee\u5f55\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>store<\/code><code>|- index.js<\/code><code>|- modules\/<\/code><code>  |- user.js<\/code><code>  |- goods.js<\/code><code><\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li><code>index.js<\/code> \u5bf9\u5916\u7684\u51fa\u53e3\uff08\u4e3b\u6587\u4ef6\uff09<\/li><li><code>modules\/user.js<\/code> \u7528\u6237\u76f8\u5173\u6a21\u5757<\/li><li><code>modules\/goods.js<\/code> \u5546\u54c1\u6a21\u5757<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>index.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import { createStore } from 'vuex'<\/code><code>import user from '.\/modules\/user'<\/code><code>import goods from '.\/modules\/goods'<\/code><code><br><\/code><code>export default createStore({<\/code><code>  state: {},<\/code><code>  getters: {},<\/code><code>  mutations: {},<\/code><code>  actions: {},<\/code><code>  modules: {<\/code><code>    user,<\/code><code>    goods<\/code><code>  }<\/code><code>})<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>user.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>const user = {<\/code><code>  state: {<\/code><code>  },<\/code><code>  getters: {<\/code><code>  },<\/code><code>  mutations: {<\/code><code>  },<\/code><code>  actions: {<\/code><code>  }<\/code><code>}<\/code><code><br><\/code><code>export default user<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>goods.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>const goods = {<\/code><code>  state: {<\/code><code>  },<\/code><code>  getters: {<\/code><code>  },<\/code><code>  mutations: {<\/code><code>  },<\/code><code>  actions: {<\/code><code>  }<\/code><code>}<\/code><code><br><\/code><code>export default goods<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5728\u5404\u4e2a\u6a21\u5757\u91cc\u653e\u5165\u76f8\u5e94\u7684\u6570\u636e\u548c\u65b9\u6cd5\u5c31\u884c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7ec4\u5efa\u4e2d\u8c03\u7528\u65b9\u6cd5\u548c\u8bbf\u95ee\u6570\u636e\uff0c\u90fd\u548c\u4e4b\u524d\u7684\u7528\u6cd5\u5dee\u4e0d\u591a\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u5c31\u662f <code>Vuex<\/code> \u7684\u57fa\u7840\u7528\u6cd5\u3002\u9664\u6b64\u4e4b\u5916\uff0c<code>Vuex<\/code> \u8fd8\u6709\u5404\u79cd\u8bed\u6cd5\u7cd6\uff0c\u5927\u5bb6\u53ef\u4ee5\u81ea\u884c\u67e5\u9605 <a href=\"https:\/\/vuex.vuejs.org\/zh\/\">\u5b98\u65b9\u6587\u6863<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Pinia<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Pinia<\/code> \u662f\u6700\u8fd1\u6bd4\u8f83\u706b\u70ed\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u4e5f\u662f\u7528\u6765\u5904\u7406 <strong>\u8de8\u7ec4\u4ef6\u901a\u4fe1<\/strong> \u7684\uff0c\u6781\u5927\u53ef\u80fd\u6210\u4e3a <code>Vuex 5<\/code> \u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pinia.vuejs.org\/\">Pinia \u6587\u6863<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u6211\u4f7f\u7528 <code>Pinia<\/code> \u4e00\u9635\u540e\u7684\u89d2\u5ea6\u6765\u770b\uff0c<code>Pinia<\/code> \u8ddf <code>Vuex<\/code> \u76f8\u6bd4\u6709\u4ee5\u4e0b\u4f18\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u8c03\u7528\u65f6\u4ee3\u7801\u8ddf\u7b80\u6d01\u4e86\u3002<\/li><li>\u5bf9 <code>TS<\/code> \u66f4\u53cb\u597d\u3002<\/li><li>\u5408\u5e76\u4e86 <code>Vuex<\/code> \u7684 <code>Mutation<\/code> \u548c <code>Action<\/code> \u3002\u5929\u7136\u7684\u652f\u6301\u5f02\u6b65\u4e86\u3002<\/li><li>\u5929\u7136\u5206\u5305\u3002<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u9664\u6b64\u4e4b\u5916\uff0c<code>Pinia<\/code> \u5b98\u7f51\u8fd8\u8bf4\u5b83\u9002\u7528\u4e8e <code>Vue2<\/code> \u548c <code>Vue3<\/code>\u3002\u4f46\u6211\u6ca1\u8bd5\u8fc7\u5728 <code>Vue2<\/code> \u4e2d\u4f7f\u7528 <s>\u6211\u61d2\u5f97\u8bd5<\/s>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Pinia<\/code> \u7b80\u5316\u4e86\u72b6\u6001\u7ba1\u7406\u6a21\u5757\uff0c\u53ea\u7528\u8fd9 3 \u4e2a\u4e1c\u897f\u5c31\u80fd\u5e94\u5bf9\u65e5\u5e38\u5927\u591a\u4efb\u52a1\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>state<\/code>\uff1a\u5b58\u50a8\u6570\u636e\u7684\u4ed3\u5e93<\/li><li><code>getters<\/code>\uff1a\u83b7\u53d6\u548c\u8fc7\u6ee4\u6570\u636e\uff08\u8ddf <code>computed<\/code> \u6709\u70b9\u50cf\uff09<\/li><li><code>actions<\/code>\uff1a\u5b58\u653e \u201c\u4fee\u6539 <code>state<\/code> \u201d\u7684\u65b9\u6cd5<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>npm install pinia<\/code><code><br><\/code><code># \u6216<\/code><code><br><\/code><code>yarn add pinia<\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u518c<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728 <code>src<\/code> \u76ee\u5f55\u4e0b\u521b\u5efa <code>store<\/code> \u76ee\u5f55\uff0c\u518d\u5728 <code>store<\/code> \u91cc\u521b\u5efa <code>index.js<\/code> \u548c <code>user.js<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u76ee\u5f55\u7ed3\u6784\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>store<\/code><code>|- index.js<\/code><code>|- user.js<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>index.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import { createPinia } from 'pinia'<\/code><code><br><\/code><code>const store = createPinia()<\/code><code><br><\/code><code>export default store<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>user.js<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u89c1\u7684\u5199\u6cd5\u6709 2 \u79cd\uff0c\u9009\u5176\u4e2d\u4e00\u79cd\u5c31\u884c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import { defineStore } from 'pinia'<\/code><code><br><\/code><code>\/\/ \u5199\u6cd51<\/code><code>export const useUserStore = defineStore({<\/code><code>  id: 'user', \/\/ id\u5fc5\u586b\uff0c\u4e14\u9700\u8981\u552f\u4e00<\/code><code>  state: () =&gt; {<\/code><code>    return {<\/code><code>      name: '\u96f7\u7334'<\/code><code>    }<\/code><code>  },<\/code><code>  getters: {<\/code><code>    fullName: (state) =&gt; {<\/code><code>      return '\u6211\u53eb ' + state.name<\/code><code>    }<\/code><code>  },<\/code><code>  actions: {<\/code><code>    updateName(name) {<\/code><code>      this.name = name<\/code><code>    }<\/code><code>  }<\/code><code>})<\/code><code><br><\/code><code><br><\/code><code>\/\/ \u5199\u6cd52<\/code><code>export const useUserStore = defineStore('user',{<\/code><code>  state: () =&gt; {<\/code><code>    return {<\/code><code>      name: '\u96f7\u7334'<\/code><code>    }<\/code><code>  },<\/code><code>  getters: {<\/code><code>    fullName: (state) =&gt; {<\/code><code>      return '\u6211\u53eb ' + state.name<\/code><code>    }<\/code><code>  },<\/code><code>  actions: {<\/code><code>    updateName(name) {<\/code><code>      this.name = name<\/code><code>    }<\/code><code>  }<\/code><code>})<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7136\u540e\u5728 <code>src\/main.js<\/code> \u4e2d\u5f15\u5165 <code>store\/index.js<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>src\/main.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>import { createApp } from 'vue'<\/code><code>import App from '.\/App.vue'<\/code><code>import store from '.\/store'<\/code><code><br><\/code><code>const app = createApp(App)<\/code><code><br><\/code><code>app<\/code><code>  .use(store)<\/code><code>  .mount('#app')<\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5728\u7ec4\u4ef6\u4e2d\u4f7f\u7528<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7ec4\u4ef6<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ xxx.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    &lt;div&gt;name: {{ name }}&lt;\/div&gt;<\/code><code>    &lt;div&gt;\u5168\u540d\uff1a{{ fullName }}&lt;\/div&gt;<\/code><code>    &lt;button @click=\"handleClick\"&gt;\u4fee\u6539&lt;\/button&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import { computed } from 'vue'<\/code><code>import { storeToRefs } from 'pinia'<\/code><code>import { useUserStore } from '@\/store\/user'<\/code><code><br><\/code><code>const userStore = useUserStore()<\/code><code><br><\/code><code>\/\/ const name = computed(() =&gt; userStore.name)<\/code><code><br><\/code><code>\/\/ \u5efa\u8bae<\/code><code>const { name, fullName } = storeToRefs(userStore)<\/code><code><br><\/code><code><br><\/code><code>function handleClick() {<\/code><code>  \/\/ \u4e0d\u5efa\u8bae\u8fd9\u6837\u6539<\/code><code>  \/\/ name.value = '\u874e\u5b50\u83b1\u83b1'<\/code><code><br><\/code><code>  \/\/ \u63a8\u8350\u7684\u5199\u6cd5\uff01\uff01\uff01<\/code><code>  userStore.updateName('\u674e\u56db')<\/code><code>}<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;br&gt;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5570\u55e6\u4e24\u53e5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5176\u5b9e <code>Pinia<\/code> \u7684\u7528\u6cd5\u548c <code>Vuex<\/code> \u662f\u633a\u50cf\u7684\uff0c\u9ed8\u8ba4\u5c31\u662f\u5206\u5305\u7684\u903b\u8f91\uff0c\u5728\u8fd9\u65b9\u9762\u6211\u652f\u6301 <strong>\u83e0\u841d(Pinia)<\/strong>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Pinia<\/code> \u8fd8\u63d0\u4f9b\u4e86\u591a\u79cd\u8bed\u6cd5\u7cd6\uff0c\u5f3a\u70c8\u5efa\u8bae\u9605\u8bfb\u4e00\u4e0b <a href=\"https:\/\/pinia.vuejs.org\/\">\u5b98\u65b9\u6587\u6863<\/a>\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">mitt.js<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u4eec\u524d\u9762\u7528\u5230\u7684 <strong>\u603b\u7ebf Bus<\/strong> \u65b9\u6cd5\uff0c\u5176\u5b9e\u548c <code>mitt.js<\/code> \u6709\u70b9\u50cf\uff0c\u4f46 <code>mitt.js<\/code> \u63d0\u4f9b\u4e86\u66f4\u591a\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6bd4\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>on<\/code>\uff1a\u6dfb\u52a0\u4e8b\u4ef6<\/li><li><code>emit<\/code>\uff1a\u6267\u884c\u4e8b\u4ef6<\/li><li><code>off<\/code>\uff1a\u79fb\u9664\u4e8b\u4ef6<\/li><li><code>clear<\/code>\uff1a\u6e05\u9664\u6240\u6709\u4e8b\u4ef6<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mitt.js<\/code> \u4e0d\u662f\u4e13\u95e8\u7ed9 <code>Vue<\/code> \u670d\u52a1\u7684\uff0c\u4f46 <code>Vue<\/code> \u53ef\u4ee5\u5229\u7528 <code>mitt.js<\/code> \u505a\u8de8\u7ec4\u4ef6\u901a\u4fe1\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/developit\/mitt\">github \u5730\u5740<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.npmjs.com\/package\/mitt\">npm \u5730\u5740<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b89\u88c5<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>npm i mitt <\/code><code><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u6a21\u62df\u4e00\u4e0b <strong>\u603b\u7ebf Bus<\/strong> \u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u5728\u540c\u7ea7\u76ee\u5f55\u521b\u5efa 3 \u4e2a\u6587\u4ef6\u7528\u4f5c\u6a21\u62df\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>Parent.vue<\/code><code>Child.vue<\/code><code>Bus.js<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Bus.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Bus.js<\/code><code><br><\/code><code>import mitt from 'mitt'<\/code><code>export default mitt()<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parent.vue<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Parent.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    Mitt<\/code><code>    &lt;Child \/&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import Child from '.\/Child.vue'<\/code><code>import Bus from '.\/Bus.js'<\/code><code><br><\/code><code>Bus.on('sayHello', () =&gt; console.log('\u96f7\u7334\u554a'))<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Child.vue<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/\/ Child.vue<\/code><code><br><\/code><code>&lt;template&gt;<\/code><code>  &lt;div&gt;<\/code><code>    Child\uff1a&lt;button @click=\"handleClick\"&gt;\u6253\u58f0\u62db\u547c&lt;\/button&gt;<\/code><code>  &lt;\/div&gt;<\/code><code>&lt;\/template&gt;<\/code><code><br><\/code><code>&lt;script setup&gt;<\/code><code>import Bus from '.\/Bus.js'<\/code><code><br><\/code><code>function handleClick() {<\/code><code>  Bus.emit('sayHello')<\/code><code>}<\/code><code>&lt;\/script&gt;<\/code><code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6b64\u65f6\uff0c\u70b9\u51fb <code>Child.vue<\/code> \u4e0a\u7684\u6309\u94ae\uff0c\u5728\u63a7\u5236\u53f0\u5c31\u4f1a\u6267\u884c\u5728 <code>Parent.vue<\/code> \u91cc\u5b9a\u4e49\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>mitt.js<\/code> \u7684\u7528\u6cd5\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u5efa\u8bae\u8ddf\u7740 <a href=\"https:\/\/www.npmjs.com\/package\/mitt\">\u5b98\u65b9\u793a\u4f8b<\/a> \u6572\u4e00\u4e0b\u4ee3\u7801\uff0c\u51e0\u5206\u949f\u5c31\u4e0a\u624b\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6587\u7ae0\u6765\u6e90\uff1a<a href=\"https:\/\/xie.infoq.cn\/article\/34871565a5356b0e58bc49d6c\">https:\/\/xie.infoq.cn\/article\/34871565a5356b0e58bc49d6c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f17\u6240\u5468\u77e5\uff0cVue.js \u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\u662f\u7ec4\u4ef6\u901a\u4fe1\uff0c\u4e0d\u7ba1\u662f\u4e1a\u52a1\u7c7b\u7684\u5f00\u53d1\u8fd8\u662f\u7ec4\u4ef6\u5e93\u5f00\u53d1\uff0c\u90fd\u6709\u5404\u81ea\u7684\u901a\u8baf\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":2,"featured_media":168,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"inline_featured_image":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[12],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-12","col-md-12"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f - WITE<\/title>\n<meta name=\"description\" content=\"\u4f17\u6240\u5468\u77e5\uff0cVue.js \u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\u662f\u7ec4\u4ef6\u901a\u4fe1\uff0c\u4e0d\u7ba1\u662f\u4e1a\u52a1\u7c7b\u7684\u5f00\u53d1\u8fd8\u662f\u7ec4\u4ef6\u5e93\u5f00\u53d1\uff0c\u90fd\u6709\u5404\u81ea\u7684\u901a\u8baf\u65b9\u6cd5\u3002\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/wite.oxoooo.com\/?p=167\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f - WITE\" \/>\n<meta property=\"og:description\" content=\"\u4f17\u6240\u5468\u77e5\uff0cVue.js \u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\u662f\u7ec4\u4ef6\u901a\u4fe1\uff0c\u4e0d\u7ba1\u662f\u4e1a\u52a1\u7c7b\u7684\u5f00\u53d1\u8fd8\u662f\u7ec4\u4ef6\u5e93\u5f00\u53d1\uff0c\u90fd\u6709\u5404\u81ea\u7684\u901a\u8baf\u65b9\u6cd5\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/wite.oxoooo.com\/?p=167\" \/>\n<meta property=\"og:site_name\" content=\"WITE\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-04T00:17:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-23T07:28:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/wite.oxoooo.com\/wp-content\/uploads\/2022\/03\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"396\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"hgostand\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"hgostand\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167\"},\"author\":{\"name\":\"hgostand\",\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/#\\\/schema\\\/person\\\/00c6377b90c41a8d0789bd572010a7ea\"},\"headline\":\"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f\",\"datePublished\":\"2022-03-04T00:17:46+00:00\",\"dateModified\":\"2022-03-23T07:28:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167\"},\"wordCount\":120,\"publisher\":{\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/#\\\/schema\\\/person\\\/00c6377b90c41a8d0789bd572010a7ea\"},\"image\":{\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wite.oxoooo.com\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg\",\"articleSection\":[\"\u793a\u4f8b\u4ee3\u7801\"],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167\",\"url\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167\",\"name\":\"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f - WITE\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/wite.oxoooo.com\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg\",\"datePublished\":\"2022-03-04T00:17:46+00:00\",\"dateModified\":\"2022-03-23T07:28:43+00:00\",\"description\":\"\u4f17\u6240\u5468\u77e5\uff0cVue.js \u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\u662f\u7ec4\u4ef6\u901a\u4fe1\uff0c\u4e0d\u7ba1\u662f\u4e1a\u52a1\u7c7b\u7684\u5f00\u53d1\u8fd8\u662f\u7ec4\u4ef6\u5e93\u5f00\u53d1\uff0c\u90fd\u6709\u5404\u81ea\u7684\u901a\u8baf\u65b9\u6cd5\u3002\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167#primaryimage\",\"url\":\"https:\\\/\\\/wite.oxoooo.com\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg\",\"contentUrl\":\"https:\\\/\\\/wite.oxoooo.com\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg\",\"width\":900,\"height\":396},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/?p=167#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\\\/\\\/wite.oxoooo.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/#website\",\"url\":\"https:\\\/\\\/wite.oxoooo.com\\\/\",\"name\":\"WITE\",\"description\":\"\u805a\u7126\u6280\u672f\u9886\u57df\u521b\u65b0\",\"publisher\":{\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/#\\\/schema\\\/person\\\/00c6377b90c41a8d0789bd572010a7ea\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/wite.oxoooo.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/wite.oxoooo.com\\\/#\\\/schema\\\/person\\\/00c6377b90c41a8d0789bd572010a7ea\",\"name\":\"hgostand\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\\\/\\\/www.hgostand.com\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/\u8d44\u6e90-4@4x.png\",\"url\":\"https:\\\/\\\/www.hgostand.com\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/\u8d44\u6e90-4@4x.png\",\"contentUrl\":\"https:\\\/\\\/www.hgostand.com\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/\u8d44\u6e90-4@4x.png\",\"width\":824,\"height\":618,\"caption\":\"hgostand\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.hgostand.com\\\/wp-content\\\/uploads\\\/2022\\\/03\\\/\u8d44\u6e90-4@4x.png\"},\"url\":\"https:\\\/\\\/wite.oxoooo.com\\\/?author=2\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f - WITE","description":"\u4f17\u6240\u5468\u77e5\uff0cVue.js \u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\u662f\u7ec4\u4ef6\u901a\u4fe1\uff0c\u4e0d\u7ba1\u662f\u4e1a\u52a1\u7c7b\u7684\u5f00\u53d1\u8fd8\u662f\u7ec4\u4ef6\u5e93\u5f00\u53d1\uff0c\u90fd\u6709\u5404\u81ea\u7684\u901a\u8baf\u65b9\u6cd5\u3002","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/wite.oxoooo.com\/?p=167","og_locale":"zh_CN","og_type":"article","og_title":"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f - WITE","og_description":"\u4f17\u6240\u5468\u77e5\uff0cVue.js \u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\u662f\u7ec4\u4ef6\u901a\u4fe1\uff0c\u4e0d\u7ba1\u662f\u4e1a\u52a1\u7c7b\u7684\u5f00\u53d1\u8fd8\u662f\u7ec4\u4ef6\u5e93\u5f00\u53d1\uff0c\u90fd\u6709\u5404\u81ea\u7684\u901a\u8baf\u65b9\u6cd5\u3002","og_url":"https:\/\/wite.oxoooo.com\/?p=167","og_site_name":"WITE","article_published_time":"2022-03-04T00:17:46+00:00","article_modified_time":"2022-03-23T07:28:43+00:00","og_image":[{"width":900,"height":396,"url":"https:\/\/wite.oxoooo.com\/wp-content\/uploads\/2022\/03\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg","type":"image\/jpeg"}],"author":"hgostand","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"hgostand","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/wite.oxoooo.com\/?p=167#article","isPartOf":{"@id":"https:\/\/wite.oxoooo.com\/?p=167"},"author":{"name":"hgostand","@id":"https:\/\/wite.oxoooo.com\/#\/schema\/person\/00c6377b90c41a8d0789bd572010a7ea"},"headline":"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f","datePublished":"2022-03-04T00:17:46+00:00","dateModified":"2022-03-23T07:28:43+00:00","mainEntityOfPage":{"@id":"https:\/\/wite.oxoooo.com\/?p=167"},"wordCount":120,"publisher":{"@id":"https:\/\/wite.oxoooo.com\/#\/schema\/person\/00c6377b90c41a8d0789bd572010a7ea"},"image":{"@id":"https:\/\/wite.oxoooo.com\/?p=167#primaryimage"},"thumbnailUrl":"https:\/\/wite.oxoooo.com\/wp-content\/uploads\/2022\/03\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg","articleSection":["\u793a\u4f8b\u4ee3\u7801"],"inLanguage":"zh-Hans"},{"@type":"WebPage","@id":"https:\/\/wite.oxoooo.com\/?p=167","url":"https:\/\/wite.oxoooo.com\/?p=167","name":"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f - WITE","isPartOf":{"@id":"https:\/\/wite.oxoooo.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/wite.oxoooo.com\/?p=167#primaryimage"},"image":{"@id":"https:\/\/wite.oxoooo.com\/?p=167#primaryimage"},"thumbnailUrl":"https:\/\/wite.oxoooo.com\/wp-content\/uploads\/2022\/03\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg","datePublished":"2022-03-04T00:17:46+00:00","dateModified":"2022-03-23T07:28:43+00:00","description":"\u4f17\u6240\u5468\u77e5\uff0cVue.js \u4e2d\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u77e5\u8bc6\u70b9\u662f\u7ec4\u4ef6\u901a\u4fe1\uff0c\u4e0d\u7ba1\u662f\u4e1a\u52a1\u7c7b\u7684\u5f00\u53d1\u8fd8\u662f\u7ec4\u4ef6\u5e93\u5f00\u53d1\uff0c\u90fd\u6709\u5404\u81ea\u7684\u901a\u8baf\u65b9\u6cd5\u3002","breadcrumb":{"@id":"https:\/\/wite.oxoooo.com\/?p=167#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/wite.oxoooo.com\/?p=167"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/wite.oxoooo.com\/?p=167#primaryimage","url":"https:\/\/wite.oxoooo.com\/wp-content\/uploads\/2022\/03\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg","contentUrl":"https:\/\/wite.oxoooo.com\/wp-content\/uploads\/2022\/03\/srchttp___blog.virtualman.top_wp-content_uploads_2021_08_vue.jpgreferhttp___blog.virtualman.jpg","width":900,"height":396},{"@type":"BreadcrumbList","@id":"https:\/\/wite.oxoooo.com\/?p=167#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/wite.oxoooo.com\/"},{"@type":"ListItem","position":2,"name":"Vue3\u5341\u79cd\u7ec4\u4ef6\u901a\u8baf\u65b9\u5f0f"}]},{"@type":"WebSite","@id":"https:\/\/wite.oxoooo.com\/#website","url":"https:\/\/wite.oxoooo.com\/","name":"WITE","description":"\u805a\u7126\u6280\u672f\u9886\u57df\u521b\u65b0","publisher":{"@id":"https:\/\/wite.oxoooo.com\/#\/schema\/person\/00c6377b90c41a8d0789bd572010a7ea"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/wite.oxoooo.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/wite.oxoooo.com\/#\/schema\/person\/00c6377b90c41a8d0789bd572010a7ea","name":"hgostand","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.hgostand.com\/wp-content\/uploads\/2022\/03\/\u8d44\u6e90-4@4x.png","url":"https:\/\/www.hgostand.com\/wp-content\/uploads\/2022\/03\/\u8d44\u6e90-4@4x.png","contentUrl":"https:\/\/www.hgostand.com\/wp-content\/uploads\/2022\/03\/\u8d44\u6e90-4@4x.png","width":824,"height":618,"caption":"hgostand"},"logo":{"@id":"https:\/\/www.hgostand.com\/wp-content\/uploads\/2022\/03\/\u8d44\u6e90-4@4x.png"},"url":"https:\/\/wite.oxoooo.com\/?author=2"}]}},"_links":{"self":[{"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=167"}],"version-history":[{"count":4,"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":200,"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=\/wp\/v2\/posts\/167\/revisions\/200"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=\/wp\/v2\/media\/168"}],"wp:attachment":[{"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wite.oxoooo.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}