Vue2子組件綁定 v-model,實(shí)現(xiàn)父子組件通信方式
前言
v-model 可以在組件上使用以實(shí)現(xiàn)雙向綁定。
首先讓我們回憶一下 v-model 在原生元素上的用法:
<input v-model="firstName" />
在代碼背后,模板編譯器會對 v-model 進(jìn)行更冗長的等價展開。因此上面的代碼其實(shí)等價于下面這段:
<input :value="firstName" @input="firstName = $event.target.value" />
默認(rèn)用法
父組件
<template>
<div>
<h1>{{ first }}-{{ last }}</h1>
<UserName
:firstName="first"
:lastName="last"
@update:firstName="func1"
@update:lastName="func2"
/>
</div>
</template>
<script>
import UserName from "./UserName.vue";
export default {
name: "V-Model",
components: {
UserName,
},
data() {
return {
first: "000",
last: "123",
};
},
methods: {
// 默認(rèn)用法
func1(val) {
this.first = val;
},
func2(val) {
this.last = val;
},
},
};
</script>子組件
<template>
<div>
<input v-model="first" type="text" @input="input1" />
<input v-model="last" type="text" @input="input2" />
</div>
</template>
<script>
export default {
name: "UserName",
props: ["firstName", "lastName"],
data() {
return {
first: this.firstName,
last: this.lastName,
};
},
methods: {
input1() {
this.$emit("update:firstName", this.first);
},
input2() {
this.$emit("update:lastName", this.last);
},
},
};
</script>以上就可以實(shí)現(xiàn) 父子組件的雙向綁定

.sync寫法
傳參的時候加上 .sync 會簡化上面的寫法,父組件不需要定義更新觸發(fā)函數(shù)(update)
<UserName :firstName.sync="first" />
會被擴(kuò)展為:
<UserName :firstName="first" @update:firstName="val => first = val"></UserName>
當(dāng)子組件需要更新 firstName 的值時,它需要顯式地觸發(fā)一個更新事件:
this.$emit('update:firstName', newValue)父組件
<template>
<div>
<h1>{{ first }}-{{ last }}</h1>
<UserName :firstName.sync="first" :lastName.sync="last" />
</div>
</template>
<script>
import UserName from "./UserName.vue";
export default {
name: "V-Model",
components: {
UserName,
},
data() {
return {
first: "000",
last: "123",
};
},
methods: {
},
};
</script>子組件
<template>
<div>
<input type="text" :value="firstName" @input="$emit('update:firstName', $event.target.value)" />
<input type="text" :value="lastName" @input="$emit('update:lastName', $event.target.value)" />
</div>
</template>
<script>
export default {
name: "UserName",
components: {},
props: ["firstName", "lastName"],
data() {
return {};
},
methods: {},
};
</script>以上也可以實(shí)現(xiàn) 父子組件的雙向綁定

綁定單個 v-model
當(dāng)使用在一個組件上時,v-model 會被展開為如下的形式:
<UserName v-model="first" /> <!-- 上面等同于下面的寫法 --> <UserName :modelValue="first" @input="first= $event.target.value" />
父組件
<template>
<div>
<h1>{{ first }}-{{ last }}</h1>
<UserName v-model="first" />
</div>
</template>
<script>
import UserName from "./UserName.vue";
export default {
name: "V-Model",
components: {
UserName,
},
data() {
return {
first: "000",
last: "123",
};
},
methods: {
},
};
</script>子組件
<template>
<div>
<input
type="text"
:value="firstName"
@input="$emit('update', $event.target.value)"
/>
</div>
</template>
<script>
export default {
name: "UserName",
components: {},
props: ["firstName"],
model: {
prop: "firstName",
event: "update",
},
data() {
return {};
},
};
</script>現(xiàn)在可以實(shí)現(xiàn)單個 輸入框綁定

總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
- 在Vue2中v-model和.sync區(qū)別解析
- vue2和vue3組件v-model區(qū)別詳析
- vue2中如何自定義組件的v-model
- Vue v-model相關(guān)知識總結(jié)
- vue2 v-model/v-text 中使用過濾器的方法示例
- vue 2.0組件與v-model詳解
- Vue2.0利用 v-model 實(shí)現(xiàn)組件props雙向綁定的優(yōu)美解決方案
- vue2 如何實(shí)現(xiàn)div contenteditable=“true”(類似于v-model)的效果
- vue2與vue3雙向數(shù)據(jù)綁定的區(qū)別說明
- vue 2 實(shí)現(xiàn)自定義組件一到多個v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
相關(guān)文章
Vue3.0插件執(zhí)行原理與實(shí)戰(zhàn)
這篇文章主要介紹了Vue3.0插件執(zhí)行原理與實(shí)戰(zhàn),Vue項(xiàng)目能夠使用很多插件來豐富自己的功能Vue-Router、Vuex等,節(jié)省了我們大量的人力和物力,下面我們就一起來了解Vue3.0插件的原理吧,需要的小伙伴可以參考一下2022-02-02
100行代碼理解和分析vue2.0響應(yīng)式架構(gòu)
通過100行代碼幫助大家理解和分析vue2.0響應(yīng)式架構(gòu)的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
vue項(xiàng)目優(yōu)雅實(shí)現(xiàn)自動引入組件的方法詳解
在我們寫vue項(xiàng)目的時候,都會引入一些組件庫,有時候有可能還不止一個組件庫,那么如何優(yōu)雅的實(shí)現(xiàn)自動引入組件呢,下面小編就來和大家詳細(xì)講講吧2023-09-09
vue自定義鍵盤信息、監(jiān)聽數(shù)據(jù)變化的方法示例【基于vm.$watch】
這篇文章主要介紹了vue自定義鍵盤信息、監(jiān)聽數(shù)據(jù)變化的方法,結(jié)合實(shí)例形式分析了vue.js基于vm.$watch進(jìn)行事件監(jiān)聽相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
vue3使用Electron打包成exe的方法與打包報錯解決
在前端開發(fā)中,Electron是一種常用的工具,它允許開發(fā)者使用Web技術(shù)構(gòu)建桌面應(yīng)用程序,本文主要介紹了vue3使用Electron打包成exe的方法與打包報錯解決,具有一定的參考價值,感興趣的可以了解一下2024-06-06

