Vue3 defineModel的使用示例詳解
基礎(chǔ)配置使用
在vue中,父組件可以在子組件上傳值,子組件通過props接收,但是子組件不可修改
props,否則控制臺會報出警告,這也符合vue的單向數(shù)據(jù)流
原則,所以如果想要向子組件傳值,并且允許子組件修改的話,一般會這么寫:
<!-- Child.vue --> <script setup> const props = defineProps(['modelValue']) const emit = defineEmits(['update:modelValue']) </script> <template> <input :value="props.modelValue" @input="emit('update:modelValue', $event.target.value)" /> </template>
在父組件調(diào)用
<!-- Parent.vue --> <Child :modelValue="foo" @update:modelValue="$event => (foo = $event)" />
而在vue3中,聲明了一種新的傳值方式defineModel,可以更方便的向子組件傳值,并且允許子組件修改傳值,以實(shí)現(xiàn)雙向綁定.
<!-- Child.vue --> <script setup> const model = defineModel() function update() { model.value++ } </script> <template> <div>Parent bound v-model is: {{ model }}</div> <button @click="update">Increment</button> </template>
父組件
<!-- Parent.vue --> <Child v-model="countModel" />
defineModel() 返回的值是一個 ref。它可以像其他 ref一樣被訪問以及修改,不過它能起到在父組件和當(dāng)前變量之間的雙向綁定的作用:
- 它的 .value 和父組件的 v-model 的值同步;
- 當(dāng)它被子組件變更了,會觸發(fā)父組件綁定的值一起更新。
defineModel
允許配置,可以通過給 defineModel 傳遞選項(xiàng),來聲明底層 prop 的選項(xiàng):
// 使 v-model 必填 const model = defineModel({ required: true }) // 提供一個默認(rèn)值 const model = defineModel({ default: 0 })
需要注意的是:如果為 defineModel prop 設(shè)置了一個 default 值且父組件沒有為該 prop 提供任何值,會導(dǎo)致父組件與子組件之間不同步。
<!-- Child.vue --> const model = defineModel({ default: 1 }) <!-- Parent.vue --> const myRef = ref() <Child v-model="myRef"></Child>
上述代碼中,父組件的 myRef 是 undefined,而子組件的 model 是 1,所以如果設(shè)置初始值
,應(yīng)保持二者一致
。
配置參數(shù),綁定多個v-model
組件上的 v-model 也可以接受一個參數(shù):
<MyComponent v-model:title="bookTitle" />
<!-- MyComponent.vue --> <script setup> const title = defineModel('title') </script> <template> <input type="text" v-model="title" /> </template>
如果需要額外的 prop 選項(xiàng),應(yīng)該在 model 名稱之后傳遞:
const title = defineModel('title', { required: true })
綁定多個v-model
<UserName v-model:first-name="first" v-model:last-name="last" />
<script setup> const firstName = defineModel('firstName') const lastName = defineModel('lastName') </script> <template> <input type="text" v-model="firstName" /> <input type="text" v-model="lastName" /> </template>
到此這篇關(guān)于Vue3 defineModel的使用示例詳解的文章就介紹到這了,更多相關(guān)Vue3 defineModel使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 報錯Error: No PostCSS Config foun
這篇文章主要介紹了Vue 報錯Error: No PostCSS Config found問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07vue自定義table表如何實(shí)現(xiàn)內(nèi)容上下循環(huán)滾動
這篇文章主要介紹了vue自定義table表如何實(shí)現(xiàn)內(nèi)容上下循環(huán)滾動問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10vue使用axios實(shí)現(xiàn)動態(tài)追加數(shù)據(jù)
在vuejs中使用axios時,有時候需要追加數(shù)據(jù),比如,移動端下拉觸底加載,分頁加載,滑動滾動條等,下面小編就來為大家介紹一下如何使用使用axios實(shí)現(xiàn)動態(tài)追加數(shù)據(jù)吧2023-10-10解決ant Design中this.props.form.validateFields未執(zhí)行的問題
這篇文章主要介紹了解決ant Design中this.props.form.validateFields未執(zhí)行的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10vue之el-upload使用FormData多文件同時上傳問題
這篇文章主要介紹了vue之el-upload使用FormData多文件同時上傳問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05Vue動態(tài)創(chuàng)建注冊component的實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Vue動態(tài)創(chuàng)建注冊component的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06el-input設(shè)置后綴顯示單位并阻止?jié)L輪微調(diào)的解決方法
在Element UI或Element Plus中,使用el-input組件時,可以通過suffix插槽添加單位顯示,如果設(shè)置了type為'number',滾輪滾動可能會導(dǎo)致數(shù)值微調(diào),解決方法是阻止?jié)L輪事件的默認(rèn)行為,并用CSS隱藏掉輸入框的上下箭頭,確保數(shù)值輸入的準(zhǔn)確性,這樣既美觀又提升了用戶體驗(yàn)2024-09-09