Vue自定義組件實(shí)現(xiàn)v-model雙向數(shù)據(jù)綁定的方法
一、Vue2 實(shí)現(xiàn)自定義組件雙向數(shù)據(jù)綁定
① v-model 實(shí)現(xiàn)雙向數(shù)據(jù)綁定
在vue2中,子組件上使用v-model的值默認(rèn)綁定到子組件的props.value屬性上,由于子組件不能改變父組件傳來(lái)的屬性,所以需要通過(guò)$emit觸發(fā)事件使得父組件中數(shù)據(jù)的變化,然后再同步到子組件。vue2默認(rèn)觸發(fā)v-model數(shù)據(jù)變化的事件為input。
使用如下:
子組件MySon
<template>
<div>
<div>雙向數(shù)據(jù)綁定:{{$props.value}}</div>
<div><button @click="addValue">點(diǎn)擊++</button></div>
</div>
</template>
?
<script>
export default {
name: "MySon",
props: ['value'],
methods: {
addValue() {
// 觸發(fā)父組件中v-model綁定數(shù)據(jù)的變化,由于不能改變props數(shù)據(jù)中的值,所以不要寫this.$props.value++這樣的操作
this.$emit('input', this.$props.value + 1)
}
}
}
</script>如果希望改變接收v-model的屬性或改變觸發(fā)v-model數(shù)據(jù)變化的事件,可通過(guò)model:{}配置實(shí)現(xiàn),如:
<template>
<div>
<div>雙向數(shù)據(jù)綁定:{{$props.value666}}</div>
<div><button @click="addValue666">點(diǎn)擊++</button></div>
</div>
</template>
?
<script>
export default {
name: "MySon",
props: ['value666'],
// --> 配置接收v-model數(shù)據(jù)的屬性以及改變數(shù)據(jù)的事件 <--
model: {
prop: 'value666',
event: 'updateValue666'
},
methods: {
addValue666() {
this.$emit('updateValue666', this.$props.value666 + 1)
}
}
}
</script>父組件
<template>
<div id="app">
<MySon v-model="num"></MySon>
</div>
</template>
?
<script>
import MySon from "@/components/MySon.vue";
export default {
name: 'App',
components: {
//注冊(cè)子組件
MySon
},
watch: {
// 監(jiān)視num數(shù)據(jù)的變化
num(newValue, oldValue) {
console.log('num: ' + oldValue + ' -> ' + newValue)
},
},
data() {
return {
num: 10,
}
},
}
</script>② .sync 實(shí)現(xiàn)子組件多個(gè)數(shù)據(jù)雙向綁定
在Vue2中使用v-model只能使用一次,如果要實(shí)現(xiàn)多個(gè)雙向數(shù)據(jù)綁定,可以借助.sync修飾,使用語(yǔ)法為 :屬性.sync="數(shù)據(jù)" ,用這種綁定代替v-model,觸發(fā)數(shù)據(jù)改變的事件為update:屬性名
使用如下:
子組件
<template>
<div>
<div>sync雙向數(shù)據(jù)綁定:{{$props.data}}</div>
<div><button @click="addData">點(diǎn)擊++</button></div>
</div>
</template>
?
<script>
export default {
name: "MySon",
// 用props.data屬性接收雙向綁定的數(shù)據(jù)
props: ['data'],
methods: {
addData() {
// 觸發(fā) update:data 事件改變父組件中綁定的值
this.$emit('update:data', this.$props.data + 1)
}
}
}
</script>父組件
<template>
<div id="app">
<!-- 用 :data.sync 將數(shù)據(jù)雙向綁定到子組件的data屬性上 -->
<MySon :data.sync="num"></MySon>
</div>
</template>
?
<script>
import MySon from "@/components/MySon.vue";
export default {
name: 'App',
components: {
MySon
},
watch: {
num(newValue, oldValue) {
console.log('num: ' + oldValue + ' -> ' + newValue)
}
},
data() {
return {
num: 10
}
},
}
</script>至于為什么子組件要通過(guò)$emit('update:屬性名', 數(shù)據(jù));來(lái)觸發(fā)父組件數(shù)據(jù)變化,原因如下:
<MySon :data.sync="num"></MySon>
||
\/
等價(jià)于
<MySon :data="num" @update:data="(newData) => {num = newData}"></MySon>二、Vue3 實(shí)現(xiàn)雙向數(shù)據(jù)綁定
在Vue3中,v-model可以實(shí)現(xiàn)多個(gè)數(shù)據(jù)雙向數(shù)據(jù)綁定,同時(shí).sync修飾符已經(jīng)不再生效。
① v-model 實(shí)現(xiàn)雙向數(shù)據(jù)綁定
vue3中子組件上使用v-model綁定的數(shù)據(jù)默認(rèn)傳到子組件的props.modelValue屬性上(vue2是props.value屬性),并且默認(rèn)觸發(fā)數(shù)據(jù)變化的事件為update:modelValue (vue2為input)
使用如下:
子組件
<template>
<div>
<div>雙向數(shù)據(jù)綁定modelValue:{{props.modelValue}}</div>
<div><button @click="addModelValue">點(diǎn)擊++</button></div>
</div>
</template>
<script setup>
// props.modelValue接收v-model綁定的數(shù)據(jù)
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
function addModelValue(){
// 觸發(fā)父組件中雙向綁定數(shù)據(jù)的變化
emit('update:modelValue', props.modelValue + 1)
}
</script>父組件
<template>
<Son v-model="num"></Son>
</template>
?
<script setup>
import {ref, watch} from "vue";
import Son from "@/components/Son.vue";
const num = ref(0)
// 監(jiān)視num數(shù)據(jù)變化
watch(num, (newValue, oldValue) => {
console.log('num: ' + oldValue + '->' + newValue)
})
</script>② v-model: 屬性 實(shí)現(xiàn)多個(gè)數(shù)據(jù)雙向綁定
數(shù)據(jù)綁定語(yǔ)法:v-model:屬性="數(shù)據(jù)"
觸發(fā)數(shù)據(jù)變化的事件:update:屬性
使用如下:
子組件
<template>
<div>
<div>雙向數(shù)據(jù)綁定data:{{props.data}}</div>
<div><button @click="addData">點(diǎn)擊++</button></div>
</div>
</template>
?
<script setup>
const props = defineProps(['data'])
const emit = defineEmits(['update:data'])
const addData = () => {
emit('update:data', props.data + 1)
}
</script>父組件
<template>
<!-- 將num數(shù)據(jù)綁定到子組件的data屬性上 -->
<Son v-model:data="num"></Son>
</template>
<script setup>
import {ref, watch} from "vue";
import Son from "@/components/Son.vue";
const num = ref(0)
watch(num, (newValue, oldValue) => {
console.log('num: ' + oldValue + '->' + newValue)
})
</script>以上就是Vue自定義組件實(shí)現(xiàn)v-model雙向數(shù)據(jù)綁定的方法的詳細(xì)內(nèi)容,更多關(guān)于Vue v-model雙向數(shù)據(jù)綁定的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue循環(huán)中多個(gè)input綁定指定v-model實(shí)例
這篇文章主要介紹了Vue循環(huán)中多個(gè)input綁定指定v-model實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
詳解element-ui動(dòng)態(tài)限定的日期范圍選擇器代碼片段
這篇文章主要介紹了element-ui動(dòng)態(tài)限定的日期范圍選擇器代碼片段,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
關(guān)于vue3+echart5?遇到的坑?Cannot?read?properties?of?undefine
這篇文章主要介紹了vue3+echart5?遇到的坑?Cannot?read?properties?of?undefined?(reading?'type'),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
解決vue.js中settimeout遇到的問(wèn)題(時(shí)間參數(shù)短效果不穩(wěn)定)
這篇文章主要介紹了解決vue.js中settimeout遇到的問(wèn)題(時(shí)間參數(shù)短效果不穩(wěn)定),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
vue粘貼復(fù)制功能的實(shí)現(xiàn)過(guò)程記錄
最近在項(xiàng)目中遇到點(diǎn)擊按鈕復(fù)制鏈接功能,所以這篇文章主要給大家介紹了關(guān)于vue粘貼復(fù)制功能的實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
簡(jiǎn)易vuex4核心原理及實(shí)現(xiàn)源碼分析
這篇文章主要為大家介紹了簡(jiǎn)易vuex4核心原理及實(shí)現(xiàn)源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
vue實(shí)現(xiàn)省市區(qū)的級(jí)聯(lián)選擇
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)省市區(qū)的級(jí)聯(lián)選擇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10

