vue3自定義組件之v-model實(shí)現(xiàn)父子組件雙向綁定
更新時(shí)間:2022年08月09日 10:08:13 作者:Mosowe
這篇文章主要介紹了vue3自定義組件之v-model實(shí)現(xiàn)父子組件雙向綁定方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
vue3 v-model父子組件雙向綁定
vue3.x移除了vue2.x的model選項(xiàng),自定義組件雙向綁定不在使用以下方法:
// vue2的v-model雙向綁定方法
model: {
? ? ?prop: 'value', //3.x默認(rèn)值改為了modelValue
? ? ?event: 'input' //3.x默認(rèn)值改為了update:modelValue
? ?},
//使用 this.$emit('input', index);vue3.x采用以下方式(v-model默認(rèn)對應(yīng)的prop值是modelValue)
父組件:
<inpageMenus ?? ?v-model="menu" ?? ?/>
子組件:
<script>
export default {
? name: 'MenusComponent',
? props: {
? ? modelValue: {
? ? ? type: Number,
? ? ? default: 0,
? ? },
? },
? methods: {
? ? clickMenu(index) {
? ? ? this.$emit('update:modelValue', index);
? ? },
? },
};
</script>vue3.x可以綁定多個(gè)v-model
父組件:
<inpageMenus ?? ?v-model="menu" ?? ?v-model:text="text" ?? ?/>
子組件:
<script>
export default {
? name: 'MenusComponent',
? props: {
? ? modelValue: {
? ? ? type: Number,
? ? ? default: 0,
? ? },
? ? text: {
? ? ? type: String,
? ? ? default: '0',
? ? },
? },
? methods: {
? ? clickMenu(index) {
? ? ? this.$emit('update:modelValue', index);
? ? },
? ? textChange(str) {
? ? ? this.$emit('update:text', str);
? ? },
? },
};
</script>vue3.x去掉了v-model的.sync修飾符
其他的比如.trim依然可以使用,新增自定義修飾符
父組件
? ? <test ? ? ? v-model.toLowerCase="color" ? ? ? v-model:text.toUpperCase="textValue"/>
子組件
<template> ? <div class='test'> ? ? <input type="text" @input="inputf"> ? ? <input type="text" @input="input2f"> ? </div> </template>
<script>
export default {
? name: 'test',
? props: {
? ? modelValue: {
? ? ? type: String,
? ? ? default: 'eee',
? ? },
? ? modelModifiers: { // 獲取modelValue修飾符
? ? ? type: Object,
? ? ? default: () => ({}),
? ? },
? ? text: {
? ? ? type: String,
? ? ? default: 'eee',
? ? },
? ? textModifiers: { // 獲取text修飾符
? ? ? type: Object,
? ? ? default: () => ({}),
? ? },
? },
? methods: {
? ? inputf(e) {
? ? ? let { value } = e.target;
? ? ? if (this.modelModifiers.toLowerCase) {
? ? ? ? value = value.toLowerCase();
? ? ? }
? ? ? this.$emit('update:modelValue', value);
? ? },
? ? input2f(e) {
? ? ? let { value } = e.target;
? ? ? if (this.textModifiers.toUpperCase) {
? ? ? ? value = value.toUpperCase();
? ? ? }
? ? ? this.$emit('update:text', value);
? ? },
? },
};
</script>vue3 v-model使用(多個(gè) v-model 綁定)
//父組件
setup(props) {
const msg = ref("msg");
const foo = ref('123')
return{
foo,msg
}
}
<!--父組件--!>
{{modelValue}} {{msg}}
<HelloWorld v-model:foo="modelValue" v-model:message="msg"></HelloWorld>
<!--子組件--!>
<input type="text" :value="foo" @input="$emit('update:foo', $event.target.value)" />
<input type="text" :value="message" @input="$emit('update:message', $event.target.value)" />/>

自定義修飾符的用法,
根據(jù)官方
//父組件
<HelloWorld v-model.capitalize="myText"></HelloWorld>
{{myText}}
setup(){
return { myText : ref(22); } //不加ref無法做到響應(yīng)
}
//子組件
//自定義修飾符 capitalize 通過modelModifiers prop 提供給組件 忽視any
<input type="text" :value="modelValue" @input="emitValue" />
props: {
modelValue: String,
modelModifiers: {
default: () => ({ capitalize: false })
}
},
created() {
console.log(this.modelModifiers); // { capitalize: true }
},
methods: {
emitValue(e: any) {
let value: any = e.target.value;
if (this.modelModifiers.capitalize) {
value = value.charAt(0).toUpperCase() + value.slice(1);
}
this.$emit("update:modelValue", value);
}
}
//對于帶參數(shù)的 v-model 綁定
<vModel v-model:foo.capitalize="bar"></vModel>
props: {
foo: {
type: String
},
fooModifiers: {
type: String
}
}
created() {
console.log(this.fooModifiers); // { capitalize: true }
}
<input type="text" :value="foo" @input="$emit('update:foo', $event.target.value)" />
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- vue3的組件通信&v-model使用實(shí)例詳解
- vue3利用v-model實(shí)現(xiàn)父子組件之間數(shù)據(jù)同步的代碼詳解
- vue3+Element?Plus?v-model實(shí)現(xiàn)父子組件數(shù)據(jù)同步的案例代碼
- vue3中使用v-model實(shí)現(xiàn)父子組件數(shù)據(jù)同步的三種方案
- 淺析Vue3中通過v-model實(shí)現(xiàn)父子組件的雙向數(shù)據(jù)綁定及利用computed簡化父子組件雙向綁定
- Vue3 使用v-model實(shí)現(xiàn)父子組件通信(常用在組件封裝規(guī)范中)的方法
相關(guān)文章
vue-cli創(chuàng)建項(xiàng)目ERROR?in?Conflict:?Multiple?assets?emit?dif
最近vue/cli創(chuàng)建項(xiàng)目后出現(xiàn)了錯誤,下面這篇文章主要給大家介紹了關(guān)于vue-cli創(chuàng)建項(xiàng)目ERROR?in?Conflict:?Multiple?assets?emit?different?content?to?the?same?filename?index.html問題的解決辦法,需要的朋友可以參考下2023-02-02
vue使用this.$message不生效的部分原因及解決方案
這篇文章主要介紹了vue使用this.$message不生效的部分原因及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
vue如何通過點(diǎn)擊事件實(shí)現(xiàn)頁面跳轉(zhuǎn)詳解
頁面跳轉(zhuǎn),我們一般都通過路由跳轉(zhuǎn)實(shí)現(xiàn),通常情況下可直接使用router-link標(biāo)簽實(shí)現(xiàn)頁面跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于vue如何通過點(diǎn)擊事件實(shí)現(xiàn)頁面跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下2022-07-07

