Vue3?使用v-model實現(xiàn)父子組件通信的方法(常用在組件封裝規(guī)范中)
歷史小劇場
歷史告訴我們,痞子就算混一輩子,也還是痞子,滑頭,最后只能滑自己。長得帥,不能當(dāng)飯吃。
成大器者的唯一要訣,是能吃虧。
吃虧就是占便宜,原先我不信,后來我信了,相當(dāng)靠譜。----《明朝那些事兒》
概述
v-mode實現(xiàn)父子組件數(shù)據(jù)同步原理主要分為:
- 父組件添加modelValue綁定數(shù)據(jù)且傳遞到子組件,然后綁定@update:modelValue事件接收子組件傳過來的值
- 子組件內(nèi)部使用defineProps來接收父組件modelValue傳過來的值,使用defineEmits自定義事件修改值然后觸發(fā)父組件@update綁定的事件
同步單個數(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>賬號: {{ 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>同步多個數(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>賬號: {{ 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實現(xiàn)父子組件通信(常用在組件封裝規(guī)范中)的文章就介紹到這了,更多相關(guān)Vue3 v-model父子組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue下如何利用canvas實現(xiàn)在線圖片標(biāo)注
這篇文章主要介紹了vue下如何利用canvas實現(xiàn)在線圖片標(biāo)注,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04
Vue項目markdown內(nèi)容預(yù)覽顯示優(yōu)化方式
在Vue項目中使用markdown-it組件預(yù)覽Markdown內(nèi)容時,通過安裝pnpm和markdown-it-github-markdown-css依賴,并重寫markdown部分樣式,實現(xiàn)了Markdown內(nèi)容的美化,在MarkdownPreview.vue組件中,通過變量renderedMarkdown接收并渲染Markdown內(nèi)容2025-10-10
Vue ElementUi同時校驗多個表單(巧用new promise)
這篇文章主要介紹了巧用new promise實現(xiàn)Vue ElementUi同時校驗多個表單功能,實現(xiàn)的方法有很多種,本文給大家?guī)淼氖且环N比較完美的方案,需要的朋友可以參考下2018-06-06
vue axios調(diào)用接口方法報錯500 internal server err
前端使用axios 訪問后端接口時報錯,在瀏覽器中點擊紅色的報錯數(shù)據(jù),本文給大家分享vue axios調(diào)用接口方法報錯500 internal server error的兩種解決方法,感興趣的朋友一起看看吧2023-10-10

