Vue2子組件綁定 v-model,實(shí)現(xiàn)父子組件通信方式
前言
v-model 可以在組件上使用以實(shí)現(xiàn)雙向綁定。
首先讓我們回憶一下 v-model 在原生元素上的用法:
<input v-model="firstName" />
在代碼背后,模板編譯器會(huì)對(duì) v-model 進(jìn)行更冗長(zhǎng)的等價(jià)展開(kāi)。因此上面的代碼其實(shí)等價(jià)于下面這段:
<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寫(xiě)法
傳參的時(shí)候加上 .sync 會(huì)簡(jiǎn)化上面的寫(xiě)法,父組件不需要定義更新觸發(fā)函數(shù)(update)
<UserName :firstName.sync="first" />
會(huì)被擴(kuò)展為:
<UserName :firstName="first" @update:firstName="val => first = val"></UserName>
當(dāng)子組件需要更新 firstName 的值時(shí),它需要顯式地觸發(fā)一個(gè)更新事件:
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) 父子組件的雙向綁定

綁定單個(gè) v-model
當(dāng)使用在一個(gè)組件上時(shí),v-model 會(huì)被展開(kāi)為如下的形式:
<UserName v-model="first" /> <!-- 上面等同于下面的寫(xiě)法 --> <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)單個(gè) 輸入框綁定

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 在Vue2中v-model和.sync區(qū)別解析
- vue2和vue3組件v-model區(qū)別詳析
- vue2中如何自定義組件的v-model
- Vue v-model相關(guān)知識(shí)總結(jié)
- vue2 v-model/v-text 中使用過(guò)濾器的方法示例
- 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ū)別說(shuō)明
- vue 2 實(shí)現(xiàn)自定義組件一到多個(gè)v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
相關(guān)文章
vue實(shí)現(xiàn)微信分享鏈接添加動(dòng)態(tài)參數(shù)的方法
這篇文章主要介紹了vue微信分享鏈接添加動(dòng)態(tài)參數(shù)的實(shí)現(xiàn)方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-04-04
在vue+element-plus中無(wú)法同時(shí)使用v-for和v-if的問(wèn)題及解決方法
由于路由中存在不需要遍歷的數(shù)據(jù)所以像用v-if來(lái)過(guò)濾,但是報(bào)錯(cuò),百度說(shuō)vue不能同時(shí)使用v-if和v-for,今天小編給大家分享解決方式,感興趣的朋友跟隨小編一起看看吧2023-07-07
解決vue 綁定對(duì)象內(nèi)點(diǎn)擊事件失效問(wèn)題
今天小編就為大家分享一篇解決vue 綁定對(duì)象內(nèi)點(diǎn)擊事件失效問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
Vue中安裝element-ui失敗問(wèn)題原因及解決
Vue中安裝element-ui失敗問(wèn)題原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
淺談vue后臺(tái)管理系統(tǒng)權(quán)限控制思考與實(shí)踐
這篇文章主要介紹了淺談vue后臺(tái)管理系統(tǒng)權(quán)限控制思考與實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
vue如何實(shí)現(xiàn)動(dòng)態(tài)的選中狀態(tài)切換效果
這篇文章主要介紹了vue如何實(shí)現(xiàn)動(dòng)態(tài)的選中狀態(tài)切換效果,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
Vite的常見(jiàn)配置選項(xiàng)詳細(xì)說(shuō)明
相信大部分兄弟都體驗(yàn)過(guò)Vite了,都知道它很快,在學(xué)習(xí)的時(shí)候,官網(wǎng)上的各種配置也是看的眼花繚亂,不知道哪些是必要掌握的,下面這篇文章主要給大家介紹了關(guān)于Vite常見(jiàn)配置選項(xiàng)的相關(guān)資料,需要的朋友可以參考下2024-09-09

