vue3父子組件通信之子組件修改父組件傳過(guò)來(lái)的值
一、第一種,通過(guò)props方式傳值:
父組件:
父組件調(diào)用子組件Child1時(shí)通過(guò) :msg2= "msg2"把msg2的數(shù)據(jù)傳給子組件,并且通過(guò)自定義事件接收子組件的emit通知,用來(lái)修改父組件的msg2數(shù)據(jù)。
源碼:
<template>
<div>
我是home組件 父組件
<h1>{{ msg }}</h1>
<div>父組件--props方式傳值:{{ msg2 }}</div>
<Child1 :msg2="msg2" @event="eventFn" />
</div>
</template>
<script setup>
import { ref } from 'vue'
import Child1 from './child1.vue'
const props = defineProps(['msg'])
const msg2 = ref('今天是星期三,多云。')
const eventFn = (val) => {
msg2.value = val
}
</script>子組件:
子組件通過(guò)defineProps接收一下msg2 ,可以直接展示在模板上,子組件自定義emit事件去通知父組件修改msg2的數(shù)據(jù),數(shù)值是子組件傳過(guò)去的。
源碼:
<template>
<div>
<h3>大家好,我是子組件1</h3>
<button @click="handleClick">修改父組件數(shù)據(jù)msg2</button>
<div>子組件——props方式傳過(guò)來(lái):{{ msg2 }}</div>
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
const props = defineProps(['msg2'])
const emit = defineEmits(['event'])
const handleClick = () => {
emit('event', '希望早日出太陽(yáng),暴富而不是暴曬!')
}
</script>點(diǎn)擊前:

點(diǎn)擊后:

二、第二種,通過(guò)v-model+冒號(hào)+要傳的值 的方式(這個(gè)v-model可以寫多個(gè)):
父組件:
父組件調(diào)用子組件時(shí),通過(guò)v-model:num="num" 的方式傳值給子組件。
源碼:
<template>
<div>
我是home組件 父組件
<h1>{{ msg }}</h1>
<div>父組件--props方式傳值:{{ msg2 }}</div>
<div>父組件num——v-model方式傳值:{{ num }}</div>
<Child1
:msg2="msg2" @event="eventFn"
v-model:num="num"
/>
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import Child1 from './child1.vue'
const props = defineProps(['msg'])
const msg2 = ref('今天是星期三,多云。')
const num = ref(0)
const eventFn = (val) => {
msg2.value = val
}
</script>子組件:
子組件也先通過(guò)defineProps接收一下num,并展示。然后通過(guò)自定義emit事件
const emit = defineEmit(['update: num'])
然后通過(guò)點(diǎn)擊事件updateNum方法來(lái)觸發(fā)通知父組件修改num數(shù)據(jù)。
源碼:
<template>
<div>
<h3>大家好,我是子組件1</h3>
<button @click="handleClick">修改父組件數(shù)據(jù)msg2</button>
<div>子組件——props方式傳過(guò)來(lái):{{ msg2 }}</div>
<button @click="updateNum">修改父組件num</button>
<div>子組件num——v-model方式傳過(guò)來(lái):{{ num }}</div>
</div>
</template>
<script setup>
import { ref, reactive } from 'vue'
const props = defineProps(['msg2','num'])
const emit = defineEmits(['event', 'update:num'])
const handleClick = () => {
emit('event', '希望早日出太陽(yáng),暴富而不是暴曬!')
}
const updateNum = () => {
emit('update:num', 222)
}
</script>點(diǎn)擊前:

點(diǎn)擊后:

三、父組件調(diào)用子組件時(shí),通過(guò)v-model傳多個(gè)值
父組件寫法:

子組件寫法:

效果:

以上就是vue3中,props和v-model兩種常用的父子組件通信方法 。
到此這篇關(guān)于vue3父子組件通信子組件修改父組件傳過(guò)來(lái)的值的文章就介紹到這了,更多相關(guān)vue3父子組件通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE-Table上綁定Input通過(guò)render實(shí)現(xiàn)雙向綁定數(shù)據(jù)的示例
今天小編就為大家分享一篇VUE-Table上綁定Input通過(guò)render實(shí)現(xiàn)雙向綁定數(shù)據(jù)的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
vue實(shí)現(xiàn)el-select默認(rèn)選擇第一個(gè)或者第二個(gè)
這篇文章主要介紹了vue實(shí)現(xiàn)el-select默認(rèn)選擇第一個(gè)或者第二個(gè),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
關(guān)于vue中ref的使用(this.$refs獲取為undefined)
這篇文章主要介紹了關(guān)于vue中ref的使用(this.$refs獲取為undefined),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Vue生命周期activated之返回上一頁(yè)不重新請(qǐng)求數(shù)據(jù)操作
這篇文章主要介紹了Vue生命周期activated之返回上一頁(yè)不重新請(qǐng)求數(shù)據(jù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
解決vue.js 數(shù)據(jù)渲染成功仍報(bào)錯(cuò)的問(wèn)題
今天小編就為大家分享一篇解決vue.js 數(shù)據(jù)渲染成功仍報(bào)錯(cuò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Vue封裝通過(guò)API調(diào)用的組件的方法詳解
在日常業(yè)務(wù)開發(fā)中我們會(huì)經(jīng)常封裝一些業(yè)務(wù)組件,下面這篇文章主要給大家介紹了關(guān)于Vue封裝通過(guò)API調(diào)用的組件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12

