Vue3中v-model語法糖的三種寫法詳解
更新時間:2024年01月19日 09:32:10 作者:nana努力學習前端
這篇文章主要為大家詳細介紹了Vue3中v-model語法糖的三種寫法,文中的示例代碼講解詳細,具有一定的借鑒價值,有需要的小伙伴可以跟隨小編一起學習一下
Vue2 中的 v-model 默認解析為 :value 和 @input
Vue3 中的 v-model 默認解析為 :modelValue 和 @update:modelValue
Vue3 中的 v-model:attr 默認解析為 :attr和 @update:attr
Vue3 第一種寫法 modelValue 和 @update:modelValue
父組件:
<script setup lang="ts">
import { ref } from 'vue'
const count = ref(10)
</script>
<template>
<son
:model-value="count"
@update:modelValue="count = $event"
></son>
</template>
子組件:
<script setup lang="ts">
// 計數(shù)器
// 通過 v-model 解析成 modelValue @update:modelValue
defineProps<{
modelValue: number
}>()
defineEmits<{
(e: 'update:modelValue', count: number): void
}>()
</script>
<template>
<div class="cp-radio-btn">
計數(shù)器:{{ modelValue }}
<button @click="$emit('update:modelValue', modelValue + 1)">+1</button>
</div>
</template>Vue3 第二種寫法 v-model
父組件:
<script setup lang="ts">
import { ref } from 'vue'
const count = ref(10)
</script>
<template>
<son v-model="count"></son>
</template>
子組件:
<script setup lang="ts">
// 計數(shù)器
// 通過 v-model 解析成 modelValue @update:modelValue
defineProps<{
modelValue: number
}>()
defineEmits<{
(e: 'update:modelValue', count: number): void
}>()
</script>
<template>
<div class="cp-radio-btn">
計數(shù)器:{{ modelValue }}
<button @click="$emit('update:modelValue', modelValue + 1)">+1</button>
</div>
</template>Vue3 第三種寫法 通過v-model:count 解析成 count @update:count
父組件:
<script setup lang="ts">
import { ref } from 'vue'
const count = ref(10)
</script>
<template>
<son v-model:count="count"></son>
</template>
子組件:
<script setup lang="ts">
// 計數(shù)器
// 通過 v-model:count 解析成 count @update:count
defineProps<{
count: number
}>()
defineEmits<{
(e: 'update:count', count: number): void
}>()
</script>
<template>
<div class="cp-radio-btn">
計數(shù)器:{{ count }}
<button @click="$emit('update:count', count + 1)">+1</button>
</div>
</template>
<style lang="scss" scoped></style>以上就是Vue3中v-model語法糖的三種寫法詳解的詳細內容,更多關于Vue3 v-model的資料請關注腳本之家其它相關文章!
您可能感興趣的文章:
- vue 2 實現(xiàn)自定義組件一到多個v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
- vue3?中v-model語法糖示例詳解
- Vue3?使用v-model實現(xiàn)父子組件通信的方法(常用在組件封裝規(guī)范中)
- vue3的組件通信&v-model使用實例詳解
- Vue3.4中v-model雙向數(shù)據(jù)綁定新玩法詳解
- vue3利用v-model實現(xiàn)父子組件之間數(shù)據(jù)同步的代碼詳解
- vue3+Element?Plus?v-model實現(xiàn)父子組件數(shù)據(jù)同步的案例代碼
- vue3組件的v-model:value與v-model的區(qū)別解析
相關文章
vue組件props不同數(shù)據(jù)類型傳參的默認值問題
這篇文章主要介紹了vue組件props不同數(shù)據(jù)類型傳參的默認值問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
vue2中如何更改el-dialog出場動畫(el-dialog彈窗組件)
el-dialog是一個十分好用的彈窗組件,但是出場動畫比較單調,于是決定自定義一個出場動畫,本文通過實例代碼圖文相結合給大家敘述下實現(xiàn)思路,感興趣的朋友一起看看吧2022-06-06
Vue-router的使用和出現(xiàn)空白頁,路由對象屬性詳解
今天小編就為大家分享一篇Vue-router的使用和出現(xiàn)空白頁,路由對象屬性詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Vite+Electron快速構建VUE3桌面應用的實現(xiàn)
本文主要介紹了Vite+Electron快速構建VUE3桌面應用的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10

