vue在自定義組件中使用v-model進(jìn)行數(shù)據(jù)綁定的方法
本文介紹了vue v-model進(jìn)行數(shù)據(jù)綁定,分享給大家,具體如下
官方例子https://vuefe.cn/v2/api/#model
有這么一句話: 默認(rèn)情況下,一個(gè)組件上的 v-model 會把 value 用作 prop 且把 input 用作 event。
示例:
先來一個(gè)組件,不用vue-model,正常父子通信
<!-- parent -->
<template>
<p class="parent">
<p>我是父親, 對兒子說: {{sthGiveChild}}</p>
<Child @returnBack="turnBack" :give="sthGiveChild"></Child>
</p>
</template>
<script>
import Child from './Child.vue';
export default {
data() {
return {
sthGiveChild: '給你100塊'
};
},
components: {
Child
},
methods: {
turnBack(val) {
this.sthGiveChild = val;
}
}
}
</script>
<!-- child -->
<template>
<p class="child">
<p>我是兒子,父親對我說: {{give}}</p>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="returnBackFn">回應(yīng)</a>
</p>
</template>
<script>
export default {
props: {
give: String
},
methods: {
returnBackFn() {
this.$emit('returnBack', '還你200塊');
}
}
}
</script>
點(diǎn)擊回應(yīng)后,父親對兒子說的話變成了兒子的回應(yīng)。兒子收到的信息也變了,實(shí)現(xiàn)通信。
改用v-model
<!-- parent -->
<template>
<p class="parent">
<p>我是父親, 對兒子說: {{sthGiveChild}}</p>
<Child v-model="sthGiveChild"></Child>
</p>
</template>
<script>
import Child from './Child.vue';
export default {
data() {
return {
sthGiveChild: '給你100塊'
};
},
components: {
Child
}
}
</script>
<!-- child -->
<template>
<p class="child">
<p>我是兒子,父親對我說: {{give}}</p>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="returnBackFn">回應(yīng)</a>
</p>
</template>
<script>
export default {
props: {
give: String
},
model: {
prop: 'give',
event: 'returnBack'
},
methods: {
returnBackFn() {
this.$emit('returnBack', '還你200塊');
}
}
}
</script>
文案雖有不同,但是效果最終是一致的。
看看官方自定義組件的v-model
官方例子https://vuefe.cn/v2/api/#model
有這么一句話: 默認(rèn)情況下,一個(gè)組件上的 v-model 會把 value 用作 prop 且把 input 用作 event。
嘗試把上邊子組件的例子改一下,也是跑的通的
<!-- child -->
<template>
<p class="child">
<p>我是兒子,父親對我說: {{value}}</p>
<a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" @click="returnBackFn">回應(yīng)</a>
</p>
</template>
<script>
export default {
props: {
value: String
},
methods: {
returnBackFn() {
this.$emit('input', '還你200塊');
}
}
}
</script>
做一下總結(jié):
如果你懶,不想自己去處理事件,那就用默認(rèn)的 'value' && 'input' 事件去處理,如果用原生事件的,甚至連model屬性也可以省去。
如果你想自己的代碼比較明確,區(qū)分出自定義事件,那么下面的組合才是你的菜。
prop和event看你自己心情定義,當(dāng)然要知名見意【盡量避開關(guān)鍵字】
model: {
prop: 'someProp', // 注意,是prop,不帶s。我在寫這個(gè)速記的時(shí)候,多寫了一個(gè)s,調(diào)試到懷疑人生
event: 'someEvent'
}
this.$emit('someProp', [returnValueToParent])
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vxe-table?實(shí)現(xiàn)?excel?選擇一個(gè)單元格拖拽自動復(fù)制新的單元格(示例代碼)
vxe-table是一款強(qiáng)大的表格組件,支持Excel風(fēng)格的操作,通過鼠標(biāo)右下角的擴(kuò)展按鈕,用戶可以拖拽選擇單元格并自動復(fù)制內(nèi)容到擴(kuò)展區(qū)域的所有單元格中,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-01-01
VUE利用vuex模擬實(shí)現(xiàn)新聞點(diǎn)贊功能實(shí)例
本篇文章主要介紹了VUE利用vuex模擬實(shí)現(xiàn)新聞點(diǎn)贊功能實(shí)例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
解決vue3中使用echart報(bào)錯(cuò):Cannot read properties of&n
在Vue項(xiàng)目中使用Echarts進(jìn)行數(shù)據(jù)可視化是非常常見的需求,然而有時(shí)候在引入Echarts的過程中可能會遇到報(bào)錯(cuò),本文主要介紹了解決vue3中使用echart報(bào)錯(cuò):Cannot read properties of undefined (reading ‘type‘),感興趣的可以了解一下2024-01-01
vue electron應(yīng)用調(diào)exe程序的實(shí)現(xiàn)步驟
這篇文章主要介紹了vue electron應(yīng)用調(diào)exe程序的實(shí)現(xiàn)步驟,用Python寫了一個(gè)本地服務(wù)編譯成exe程序,在electron程序啟動后,自動執(zhí)行exe程序,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下2024-02-02
vue3實(shí)現(xiàn)alert自定義的plugins方式
這篇文章主要介紹了vue3實(shí)現(xiàn)alert自定義的plugins方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
vue學(xué)習(xí)筆記之Vue中css動畫原理簡單示例
這篇文章主要介紹了vue學(xué)習(xí)筆記之Vue中css動畫原理,結(jié)合簡單實(shí)例形式分析了Vue中css樣式變換動畫效果實(shí)現(xiàn)原理與相關(guān)操作技巧,需要的朋友可以參考下2020-02-02

