{"id":573,"date":"2025-12-07T09:58:51","date_gmt":"2025-12-07T01:58:51","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=573"},"modified":"2025-12-07T09:58:51","modified_gmt":"2025-12-07T01:58:51","slug":"vue%e4%b8%adv-model%e7%9a%84%e5%8f%8c%e5%90%91%e7%bb%91%e5%ae%9a%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%90%86%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/vue%e4%b8%adv-model%e7%9a%84%e5%8f%8c%e5%90%91%e7%bb%91%e5%ae%9a%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%90%86%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5\/","title":{"rendered":"Vue\u4e2dv-model\u7684\u53cc\u5411\u7ed1\u5b9a\u5b9e\u73b0\u539f\u7406\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u771f\u6b63\u5230\u4f4d\u3001\u4ece\u6e90\u7801\u539f\u7406\u5230\u6700\u4f73\u5b9e\u8df5\u5168\u8986\u76d6<\/strong> \u7684<br><strong>\u300aVue \u4e2d v-model \u7684\u53cc\u5411\u7ed1\u5b9a\u5b9e\u73b0\u539f\u7406\uff08\u542b Vue2 \/ Vue3 \u6700\u4f73\u5b9e\u8df5\uff09\u300b<\/strong>\u3002<\/p>\n\n\n\n<p>\u8fd9\u4efd\u5185\u5bb9\u4f60\u53ef\u4ee5\u76f4\u63a5\u7528\u6765\u5199\u6280\u672f\u535a\u5ba2\u3001\u9762\u8bd5\u51c6\u5907\u3001\u6216\u8005\u9879\u76ee\u4f18\u5316\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\ude80 1. v-model \u672c\u8d28\u662f\u4ec0\u4e48\uff1f<\/h1>\n\n\n\n<p>\u4e00\u53e5\u8bdd\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>v-model = value + input\uff08Vue2\uff09<\/strong><br><strong>v-model = modelValue + update:modelValue\uff08Vue3\uff09<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>\u5b83\u53ea\u662f\u4e00\u4e2a <strong>\u8bed\u6cd5\u7cd6<\/strong>\uff0c\u628a <strong>\u7236\u7ec4\u4ef6 \u2192 \u5b50\u7ec4\u4ef6\u4f20\u503c\uff08props\uff09<\/strong> \u548c<br><strong>\u5b50\u7ec4\u4ef6 \u2192 \u7236\u7ec4\u4ef6\u4e8b\u4ef6\u56de\u4f20\uff08emit\uff09<\/strong> \u7ec4\u5408\u5728\u4e00\u8d77\uff0c\u5b9e\u73b0\u53cc\u5411\u7ed1\u5b9a\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udccc 2. Vue2 &amp; Vue3 \u7684 v-model \u533a\u522b\uff08\u79d2\u61c2\u5bf9\u6bd4\uff09<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6846\u67b6<\/th><th>\u63a5\u6536 prop<\/th><th>\u89e6\u53d1\u4e8b\u4ef6<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>Vue2<\/td><td><code>value<\/code><\/td><td><code>input<\/code><\/td><td>\u65e0\u547d\u540d v-model<\/td><\/tr><tr><td>Vue3<\/td><td><code>modelValue<\/code><\/td><td><code>update:modelValue<\/code><\/td><td>\u652f\u6301\u591a v-model<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 3. Vue2 v-model \u539f\u7406\uff08\u5b8c\u6574\u8bb2\u89e3 + \u4ee3\u7801\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc \u7236\u7ec4\u4ef6\u4f7f\u7528<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;CustomInput v-model=&quot;msg&quot; \/&gt;\n\n<\/pre><\/div>\n\n\n<p>v-model \u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;CustomInput :value=&quot;msg&quot; @input=&quot;msg = $event&quot; \/&gt;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc \u5b50\u7ec4\u4ef6\uff08\u5b9e\u73b0\u53cc\u5411\u7ed1\u5b9a\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nexport default {\n  props: &#x5B;&quot;value&quot;],\n  methods: {\n    onInput(e) {\n      this.$emit(&quot;input&quot;, e.target.value);\n    }\n  }\n}\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u539f\u7406\u603b\u7ed3\uff08Vue2\uff09<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>v-model \u81ea\u52a8\u7ed9\u5b50\u7ec4\u4ef6\u4f20\u5165 <code>value<\/code><\/li>\n\n\n\n<li>\u5b50\u7ec4\u4ef6\u6539\u52a8\u540e\u53d1\u51fa <code>input<\/code> \u4e8b\u4ef6<\/li>\n\n\n\n<li>\u7236\u7ec4\u4ef6\u76d1\u542c <code>input<\/code>\uff0c\u66f4\u65b0\u7ed1\u5b9a\u53d8\u91cf<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 4. Vue3 v-model \u539f\u7406\uff08\u63a8\u8350\u5199\u6cd5\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc \u7236\u7ec4\u4ef6\u4f7f\u7528<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;MyInput v-model=&quot;msg&quot; \/&gt;\n\n<\/pre><\/div>\n\n\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;MyInput :modelValue=&quot;msg&quot; @update:modelValue=&quot;msg = $event&quot; \/&gt;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udccc \u5b50\u7ec4\u4ef6\u5199\u6cd5\uff08Vue3 \u6700\u4f73\u5b9e\u8df5\uff09<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;script setup&gt;\nconst props = defineProps({\n  modelValue: String\n})\n\nconst emit = defineEmits(&#x5B;&#039;update:modelValue&#039;])\n\nfunction onInput(e) {\n  emit(&#039;update:modelValue&#039;, e.target.value)\n}\n&amp;lt;\/script&gt;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u539f\u7406\u603b\u7ed3\uff08Vue3\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>modelValue<\/code> \u662f\u56fa\u5b9a\u5c5e\u6027\u540d<\/li>\n\n\n\n<li><code>update:modelValue<\/code> \u662f\u56fa\u5b9a\u4e8b\u4ef6\u540d<\/li>\n\n\n\n<li>\u7ec4\u4ef6\u4e2d\u53ea\u9700\u8981 emit \u5373\u53ef<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u2b50 5. Vue3 \u591a v-model\uff08\u9ad8\u7ea7\u7528\u6cd5 \/ \u9762\u8bd5\u5e38\u95ee\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;MyPick v-model:title=&quot;title&quot; v-model:checked=&quot;checked&quot; \/&gt;\n\n<\/pre><\/div>\n\n\n<p>\u7b49\u4ef7\u4e8e\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&amp;lt;MyPick \n  :title=&quot;title&quot; \n  @update:title=&quot;title = $event&quot;\n  :checked=&quot;checked&quot;\n  @update:checked=&quot;checked = $event&quot;\n\/&gt;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">\u5b50\u7ec4\u4ef6\u4ee3\u7801\uff1a<\/h3>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndefineProps(&#x5B;&#039;title&#039;, &#039;checked&#039;])\ndefineEmits(&#x5B;&#039;update:title&#039;, &#039;update:checked&#039;])\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83d\udd25 6. v-model \u7684\u6838\u5fc3\u5b9e\u73b0\u903b\u8f91\uff08\u6846\u67b6\u5e95\u5c42\uff09<\/h1>\n\n\n\n<p>Vue \u505a\u7684\u5173\u952e\u4e8b\u60c5\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u751f\u6210\u4e00\u4e2a prop\uff08value\/modelValue\uff09<\/strong><\/li>\n\n\n\n<li><strong>\u751f\u6210\u4e00\u4e2a\u4e8b\u4ef6\uff08input\/update:modelValue\uff09<\/strong><\/li>\n\n\n\n<li><strong>\u7236\u7ec4\u4ef6\u8868\u8fbe\u5f0f\u7ed1\u5b9a<\/strong>\uff08<code>msg = $event<\/code>\uff09<\/li>\n\n\n\n<li><strong>Watcher \u76d1\u542c + \u54cd\u5e94\u5f0f\u66f4\u65b0\u89c6\u56fe<\/strong><\/li>\n<\/ol>\n\n\n\n<p>\u6838\u5fc3\u601d\u60f3\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u201c\u7236\u4f20\u5c5e\u6027 + \u5b50\u89e6\u53d1\u4e8b\u4ef6 = \u53cc\u5411\u7ed1\u5b9a\u201d<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde0 7. \u5185\u90e8\u539f\u7406\uff08\u6df1\u5165\u7248\uff0c\u4f46\u901a\u4fd7\u6613\u61c2\uff09<\/h1>\n\n\n\n<h3 class=\"wp-block-heading\">Vue2 \uff1a<\/h3>\n\n\n\n<p>v-model \u7f16\u8bd1\u4e3a\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nmodel: {\n  value: (msg),\n  callback: function ($$v) { msg = $$v }\n}\n\n<\/pre><\/div>\n\n\n<p>\u5728\u751f\u6210 vnode \u65f6\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ed1\u5b9a <code>value<\/code> \u5c5e\u6027<\/li>\n\n\n\n<li>\u7ed1\u5b9a <code>input<\/code> \u76d1\u542c<\/li>\n\n\n\n<li>Watcher \u89e6\u53d1 set \u2192 notify \u66f4\u65b0 DOM<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Vue3\uff1a<\/h3>\n\n\n\n<p>\u4f7f\u7528 Proxy \u7684\u54cd\u5e94\u5f0f\u7cfb\u7edf\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst state = reactive({ msg: &quot;&quot; })\n\n<\/pre><\/div>\n\n\n<p>v-model \u7ed1\u5b9a\u7684 modelValue \u4f1a\u81ea\u52a8\u53d8\u6210 <strong>\u4f9d\u8d56\u6536\u96c6\u7684\u54cd\u5e94\u5f0f\u503c<\/strong>\u3002<\/p>\n\n\n\n<p>\u7ec4\u4ef6 emit \u66f4\u65b0 \u2192 \u5916\u5c42 state \u66f4\u65b0 \u2192 \u89e6\u53d1 effect \u2192 UI \u6e32\u67d3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83e\udde9 8. v-model \u6700\u4f73\u5b9e\u8df5\uff08\u9ad8\u7ea7\u5f00\u53d1\u5fc5\u770b\uff09<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2714 1\uff09\u7ec4\u4ef6\u5185\u90e8\u6c38\u8fdc\u4e0d\u8981\u76f4\u63a5\u4fee\u6539 props<\/h2>\n\n\n\n<p>\u9519\u8bef \u274c\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nprops.modelValue = xx\n\n<\/pre><\/div>\n\n\n<p>\u6b63\u786e \u2714\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nemit(&#039;update:modelValue&#039;, newValue)\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2714 2\uff09\u5982\u679c\u4f60\u9700\u8981\u5185\u90e8\u72b6\u6001\uff0c\u4f7f\u7528\u201c\u53d7\u63a7 + \u975e\u53d7\u63a7\u6df7\u5408\u6a21\u5f0f\u201d<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nconst innerValue = ref(props.modelValue)\n\nwatch(() =&gt; props.modelValue, v =&gt; innerValue.value = v)\n\n<\/pre><\/div>\n\n\n<p>\u9002\u7528\u4e8e\u8f93\u5165\u6846\u3001\u9632\u6296\u8bf7\u6c42\u3001\u8868\u5355\u7ec4\u4ef6\u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2714 3\uff09\u81ea\u5b9a\u4e49\u4e8b\u4ef6\u547d\u540d\u5fc5\u987b\u9075\u5b88 Vue3 \u6807\u51c6\u683c\u5f0f<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nupdate:modelValue\nupdate:xxx\n\n<\/pre><\/div>\n\n\n<p>\u5426\u5219 v-model \u4e0d\u4f1a\u751f\u6548\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2714 4\uff09\u5c01\u88c5\u8868\u5355\u7ec4\u4ef6\u65f6\uff0c\u6c38\u8fdc\u63a8\u8350\u4f7f\u7528\uff1aVue3 \u591a v-model<\/h2>\n\n\n\n<p>\u907f\u514d\u7528 <code>props.xxx<\/code> + <code>emit('xxx-update')<\/code> \u8fd9\u79cd\u8001\u6a21\u5f0f\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\ud83c\udfaf 9. \u9762\u8bd5\u56de\u7b54\u6a21\u677f\uff0830 \u79d2\u5b8c\u7f8e\u56de\u7b54\uff09<\/h1>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u8fd9\u6837\u80cc\uff1a<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5728 Vue \u4e2d v-model \u662f\u8bed\u6cd5\u7cd6\u3002<br>Vue2 \u901a\u8fc7 <code>value<\/code> + <code>input<\/code> \u5b9e\u73b0\uff0cVue3 \u901a\u8fc7 <code>modelValue<\/code> + <code>update:modelValue<\/code> \u5b9e\u73b0\u3002<br>\u672c\u8d28\u662f\u7236\u7ec4\u4ef6\u5411\u5b50\u7ec4\u4ef6\u4e0b\u53d1 props\uff0c\u5b50\u7ec4\u4ef6\u901a\u8fc7\u4e8b\u4ef6\u901a\u77e5\u7236\u7ec4\u4ef6\u66f4\u65b0\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u53cc\u5411\u7ed1\u5b9a\u3002<br>Vue3 \u652f\u6301\u591a\u4e2a v-model\uff0c\u540c\u65f6\u8bed\u4e49\u66f4\u6e05\u6670\u3002<br>\u63a8\u8350\u4f7f\u7528 emit(&#8216;update:modelValue&#8217;) \u8fd9\u79cd\u6807\u51c6\u5199\u6cd5\u3002<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><\/h1>\n","protected":false},"excerpt":{"rendered":"<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd \u771f\u6b63\u5230\u4f4d\u3001\u4ece\u6e90\u7801\u539f\u7406\u5230\u6700\u4f73\u5b9e\u8df5\u5168\u8986\u76d6 \u7684\u300aVue \u4e2d v-mode&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2025\/12\/07\/vue%e4%b8%adv-model%e7%9a%84%e5%8f%8c%e5%90%91%e7%bb%91%e5%ae%9a%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%90%86%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[],"class_list":["post-573","post","type-post","status-publish","format-standard","hentry","category-javascript"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=573"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/573\/revisions"}],"predecessor-version":[{"id":574,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/573\/revisions\/574"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}