vue3?中v-model語(yǔ)法糖示例詳解
一、 vue2 中 v-model 語(yǔ)法糖
實(shí)現(xiàn)父子組件雙向數(shù)據(jù)綁定,一個(gè)輸入框或者組件指定綁定一個(gè) v-model
1. 父組件寫法
<template>
<div>
<h1>App</h1>
<h2>{{ count }}</h2>
<input type="text" v-model="count" />
<!-- 展開寫法,@input中的 count 的值來(lái)自當(dāng)前輸入框事件 -->
<input type="text" :value="count" @input="count = $event.target.value" />
<hr />
<Children v-model="count"></Children>
<!-- 展開寫法,@input中的 count 的值來(lái)自子組件輸入框中的值 $event.target.value -->
<Children :value="count" @input="count = $event"></Children>
</div>
</template>
<script>
import Children from "@/components/Children.vue";
export default {
components: {
Children,
},
data() {
return {
count: "1",
};
},
};
</script>2. 子組件寫法
<template>
<div>
<h1>main</h1>
<div>{{ value }}</div>
<input
type="text"
:value="value"
@input="$emit('input', $event.target.value)"
/>
</div>
</template>
<script>
export default {
props: {
value: {
type: String,
},
},
};
</script>二、 vue2 的 v-bind.sync 修飾符語(yǔ)法糖,實(shí)現(xiàn)父子組件雙向數(shù)據(jù)綁定
注意:子組件標(biāo)簽中可以同時(shí)使用多個(gè) .sync 修飾符
1. 在父組件中
<template>
<div>
<h1>App</h1>
<input v-model="count" type="text" />
<hr />
<Children :count.sync="count"></Children>
<!-- 展開寫法,@update:value中的 count 的值來(lái)自子組件輸入框中的值 $event.target.value -->
<Children :count="count" @update:count="count = $event"></Children>
</div>
</template>
<script>
import Children from "@/components/Children.vue";
export default {
components: {
Children,
},
data() {
return {
count: "1",
};
},
};
</script>2. 在子組件中
<template>
<div>
<h1>main</h1>
<h2>{{ count }}</h2>
<input
type="text"
:value="count"
@input="$emit('update:count', $event.target.value)"
/>
</div>
</template>
<script>
export default {
props: {
count: {
type: String,
},
},
};
</script>三、 vue3 的 v-model 語(yǔ)法糖
vue3 中的 v-model 相當(dāng)于 vue2 中的 v-model 和 v-bind.sync 修飾符組合在一起的產(chǎn)物(擇優(yōu)整合)v-bind.sync 在 vue3 中被移除了可以在組件標(biāo)簽上使用多個(gè) v-model 綁定屬性,使用參數(shù)區(qū)分
1. 在父組件中
<template>
<div>
<h1>App</h1>
<h2>{{ count }}</h2>
<input type="text" v-model="count" />
<!-- 此展開寫法,僅限于輸入框 -->
<input type="text" :value="count" @input="count = $event.target.value" />
<hr />
<Children v-model:count="count"></Children>
<!-- 此展開寫法,僅限于組件 -->
<Children :count="count" @update:count="count = $event"></Children>
</div>
</template>
<script>
import Children from "@/components/Children.vue";
import { ref } from "vue";
export default {
components: {
Children,
},
setup() {
const count = ref("1");
return { count };
},
};
</script>2. 在子組件中
<template>
<div>
<h1>main</h1>
<div>{{ count }}</div>
<label>
count:<input
type="text"
:value="count"
@input="emit('update:count', $event.target.value)"
/>
</label>
</div>
</template>
<script>
export default {
props: {
count: {
type: String,
},
},
setup(props, { emit }) {
console.log(props);
return { emit };
},
};
</script>到此這篇關(guān)于vue3 中v-model語(yǔ)法糖的文章就介紹到這了,更多相關(guān)vue3 v-model語(yǔ)法糖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue 2 實(shí)現(xiàn)自定義組件一到多個(gè)v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
- Vue3?使用v-model實(shí)現(xiàn)父子組件通信的方法(常用在組件封裝規(guī)范中)
- vue3的組件通信&v-model使用實(shí)例詳解
- Vue3.4中v-model雙向數(shù)據(jù)綁定新玩法詳解
- vue3利用v-model實(shí)現(xiàn)父子組件之間數(shù)據(jù)同步的代碼詳解
- Vue3中v-model語(yǔ)法糖的三種寫法詳解
- vue3+Element?Plus?v-model實(shí)現(xiàn)父子組件數(shù)據(jù)同步的案例代碼
- vue3組件的v-model:value與v-model的區(qū)別解析
相關(guān)文章
Vue electron前端開啟局域網(wǎng)接口實(shí)現(xiàn)流程詳細(xì)介紹
用electron寫了一個(gè)自己用的小軟件,無(wú)后端,純本地的數(shù)據(jù)。最近想著開發(fā)一個(gè)手機(jī)端app,將PC端的數(shù)據(jù)進(jìn)行同步。為了這小小的功能單獨(dú)寫個(gè)后端又麻煩。干脆前后端不分離哈哈,直接在前端軟件中開啟接口2022-10-10
Vue3+Spring Framework框架開發(fā)實(shí)戰(zhàn)
這篇文章主要為大家介紹了Vue3+Spring Framework框架開發(fā)實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04
vue通過watch對(duì)input做字?jǐn)?shù)限定的方法
本篇文章主要介紹了vue通過watch對(duì)input做字?jǐn)?shù)限定的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
vue-router路由傳參的兩種方式詳解(params和query)
vue-router 是一個(gè)基于vue.js的路由器,它提供了強(qiáng)大的路由功能,能夠幫助開發(fā)者快速構(gòu)建單頁(yè)應(yīng)用程序,本文將詳細(xì)介紹 vue-router 路由傳參的方法,包括路由傳參的概念、路由傳參的方法、路由傳參的應(yīng)用場(chǎng)景等,需要的朋友可以參考下2024-12-12
Vuex報(bào)錯(cuò)之[vuex] unknown mutation type: han
這篇文章主要介紹了Vuex報(bào)錯(cuò)之[vuex] unknown mutation type: handlePower問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
基于Vue方法實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器
這篇文章主要為大家詳細(xì)介紹了基于Vue方法實(shí)現(xiàn)簡(jiǎn)單計(jì)時(shí)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

