Vue3?使用v-model實(shí)現(xiàn)父子組件通信的方法(常用在組件封裝規(guī)范中)
歷史小劇場(chǎng)
歷史告訴我們,痞子就算混一輩子,也還是痞子,滑頭,最后只能滑自己。長(zhǎng)得帥,不能當(dāng)飯吃。
成大器者的唯一要訣,是能吃虧。
吃虧就是占便宜,原先我不信,后來(lái)我信了,相當(dāng)靠譜。----《明朝那些事兒》
概述
v-mode實(shí)現(xiàn)父子組件數(shù)據(jù)同步原理主要分為:
- 父組件添加modelValue綁定數(shù)據(jù)且傳遞到子組件,然后綁定@update:modelValue事件接收子組件傳過(guò)來(lái)的值
- 子組件內(nèi)部使用defineProps來(lái)接收父組件modelValue傳過(guò)來(lái)的值,使用defineEmits自定義事件修改值然后觸發(fā)父組件@update綁定的事件
同步單個(gè)數(shù)據(jù)
父組件
<!-- --> <template> <div> <!-- v-model用在HTML上 --> <!-- <input type="text" v-model="username"> --> <!-- <input type="text" :value="username" @input="username = (<HTMLInputElement>$event.target)!.value" /> --> <h4>賬號(hào): {{ username }}</h4> <h4>密碼: {{ pwd }}</h4> <!-- v-model用在自定義組件上 --> <!-- <XinchaoInput v-model:username="username" v-model:pwd="pwd" /> --> <XinchaoInput :username="username" @update:username="username = $event" /> </div> </template> <script setup lang="ts"> import { ref, watch } from 'vue'; import XinchaoInput from './XinchaoInput.vue'; const username = ref<string>("張三") const pwd = ref<string>("123131") watch(username, (oldValue, newValue) => { console.log(newValue) }) </script> <style lang="scss" scoped> </style>
子組件
<!-- --> <template> <div> <input type="text" :value="username" @input="emit('update:username', (<HTMLInputElement>$event.target)!.value)" /> <!-- <br> <input type="text" :value="pwd" @input="emit('update:pwd', (<HTMLInputElement>$event.target)!.value)" /> --> </div> </template> <script setup lang="ts"> defineProps(['username', 'pwd']) const emit = defineEmits(['update:username', 'update:pwd']) </script> <style lang="scss" scoped> input { border: 2px solid #ccc; border-radius: 5px; padding: 2px; background-color: darkcyan; color: white; } </style>
同步多個(gè)數(shù)據(jù)
父組件
<!-- --> <template> <div> <!-- v-model用在HTML上 --> <!-- <input type="text" v-model="username"> --> <!-- <input type="text" :value="username" @input="username = (<HTMLInputElement>$event.target)!.value" /> --> <h4>賬號(hào): {{ username }}</h4> <h4>密碼: {{ pwd }}</h4> <!-- v-model用在自定義組件上 --> <XinchaoInput v-model:username="username" v-model:pwd="pwd" /> <!-- <XinchaoInput :username="username" @update:username="username = $event" /> --> </div> </template> <script setup lang="ts"> import { ref, watch } from 'vue'; import XinchaoInput from './XinchaoInput.vue'; const username = ref<string>("張三") const pwd = ref<string>("123131") watch(username, (oldValue, newValue) => { console.log(newValue) }) </script> <style lang="scss" scoped> </style>
子組件
<!-- --> <template> <div> <input type="text" :value="username" @input="emit('update:username', (<HTMLInputElement>$event.target)!.value)" /> <br> <input type="text" :value="pwd" @input="emit('update:pwd', (<HTMLInputElement>$event.target)!.value)" /> </div> </template> <script setup lang="ts"> defineProps(['username', 'pwd']) const emit = defineEmits(['update:username', 'update:pwd']) </script> <style lang="scss" scoped> input { border: 2px solid #ccc; border-radius: 5px; padding: 2px; background-color: darkcyan; color: white; } </style>
到此這篇關(guān)于Vue3 使用v-model實(shí)現(xiàn)父子組件通信(常用在組件封裝規(guī)范中)的文章就介紹到這了,更多相關(guān)Vue3 v-model父子組件通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue 2 實(shí)現(xiàn)自定義組件一到多個(gè)v-model雙向數(shù)據(jù)綁定的方法(最新推薦)
- vue3?中v-model語(yǔ)法糖示例詳解
- 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)文章
vue3通過(guò)render函數(shù)實(shí)現(xiàn)菜單下拉框的示例
本文主要介紹了vue3通過(guò)render函數(shù)實(shí)現(xiàn)菜單下拉框的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04Vue keep-alive實(shí)踐總結(jié)(推薦)
本篇文章主要介紹了Vue keep-alive實(shí)踐總結(jié)(推薦),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08Vue ElementUI之Form表單驗(yàn)證遇到的問(wèn)題
這篇文章主要介紹了Vue ElementUI之Form表單驗(yàn)證遇到的問(wèn)題,需要的朋友可以參考下2017-08-08基于Vue.js與WordPress Rest API構(gòu)建單頁(yè)應(yīng)用詳解
這篇文章主要介紹了基于Vue.js與WordPress Rest API構(gòu)建單頁(yè)應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09優(yōu)雅的處理vue項(xiàng)目異常實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的處理vue項(xiàng)目異常的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06關(guān)于vue-treeselect綁值、回顯等常見問(wèn)題的總結(jié)
這篇文章主要介紹了關(guān)于vue-treeselect綁值、回顯等常見問(wèn)題的總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Vue源碼解析之?dāng)?shù)據(jù)響應(yīng)系統(tǒng)的使用
這篇文章主要介紹了Vue源碼解析之?dāng)?shù)據(jù)響應(yīng)系統(tǒng)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04