Vue3中v-model語法糖的三種寫法詳解
Vue2 中的 v-model 默認(rèn)解析為 :value 和 @input
Vue3 中的 v-model 默認(rèn)解析為 :modelValue 和 @update:modelValue
Vue3 中的 v-model:attr 默認(rèn)解析為 :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語法糖的三種寫法詳解的詳細(xì)內(nèi)容,更多關(guān)于Vue3 v-model的資料請關(guān)注腳本之家其它相關(guān)文章!
- 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ū)別解析
相關(guān)文章
vue-cli下的vuex的簡單Demo圖解(實現(xiàn)加1減1操作)
這篇文章主要介紹了vue-cli下的vuex的簡單Demo(實現(xiàn)加1減1操作),本文圖文并茂給大家介紹的非常詳細(xì),具有參考借鑒價值,需要的朋友可以參考下2018-02-02Vue3 Ref獲取真實DOM學(xué)習(xí)實戰(zhàn)
這篇文章主要為大家介紹了Vue3 Ref獲取真實DOM學(xué)習(xí)實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06element ui el-date-picker組件默認(rèn)值方式
這篇文章主要介紹了element ui el-date-picker組件默認(rèn)值方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12基于vue實現(xiàn)滾動條滾動到指定位置對應(yīng)位置數(shù)字進行tween特效
這篇文章主要介紹了基于vue實現(xiàn)滾動條滾動到指定位置對應(yīng)位置數(shù)字進行tween特效,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04vue 動態(tài)給每個頁面添加title、關(guān)鍵詞和描述的方法
這篇文章主要介紹了vue 動態(tài)給每個頁面添加title、關(guān)鍵詞和描述的方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08