Vue3組件通信的具體用法實(shí)例
前言:
在組件化開發(fā)中,需要將頁面抽離成組件的形式,抽離之后就涉及到了組件中數(shù)據(jù)傳遞,可分為:父?jìng)髯樱╬rops)、子傳父(emits)、祖孫通信(provide和inject)、兄弟通信、全局通訊(pinia)。這次我就以博客的形式復(fù)習(xí)一下前三種通訊,想了解pinia可點(diǎn)擊看我前面寫的博客。
1.父?jìng)髯?/h2>
首先需要在父組件中的子組件標(biāo)簽中添加自定義屬性,將需要傳遞的值放如自定義屬性中,在子組件中通過defineProps這個(gè)方法獲取父組件傳遞過來的值。具體方法如下:
father.vue:
<template>
<div class="father">
<h2>父組件</h2>
<!-- 使用自定義屬性傳統(tǒng)數(shù)據(jù) -->
<Child :text="text" :sayHello="sayHello"></Child>
</div>
</template>
<script setup>
import { ref } from 'vue';
import Child from './Child.vue';
// state
const text = ref('我是父組件中的數(shù)據(jù)')
// function
function sayHello() {
console.log('hello');
}
</script>Chlid.vue:
<template>
<div class="child">
<div>子組件:{{ text }}</div>
<div>子內(nèi)容</div>
</div>
</template>
<script setup>
// 接收父組件傳遞來的數(shù)據(jù)
const props = defineProps({
text: String,
sayHello: Function
})
// 判斷方法是否傳遞過來,是則執(zhí)行方法
if (props.sayHello) {
props.sayHello()
}
</script>從上可以看出,不只有數(shù)據(jù)可以傳遞,方法也是可以傳遞給子組件的。子組件在接收時(shí),需要給數(shù)據(jù)標(biāo)注類型。
2.子傳父
1.defineExpose+ref
使用defineExpose+ref的方式將子組件中的數(shù)據(jù)傳遞給父組件,首先在子組件中定義數(shù)據(jù),使用defineExpose方法將數(shù)據(jù)暴露出去,在父組件中通過ref來接收。
chlid.vue:
<template>
<div class="child">
<div>子組件</div>
<div>子內(nèi)容</div>
</div>
</template>
<script setup>
import { ref } from 'vue';
// state
const childText = ref('我是子組件中的數(shù)據(jù)')
// function
function sayHelloFather() {
console.log('hello father');
}
// 將子組件中的數(shù)據(jù)暴露出去
defineExpose({
childText,
sayHelloFather
})
</script>father:
<template>
<div class="father">
<h2>父組件</h2>
<!-- 使用自定義屬性傳統(tǒng)數(shù)據(jù) -->
<Child ref="childRef"></Child>
</div>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import Child from './Child.vue';
// 接收子組件中傳遞的數(shù)據(jù)
const childRef = ref({
childText: String,
sayHelloFather: Function
})
// 打印數(shù)據(jù)
onMounted(() => {
console.log('childText:', childRef.value?.childText);
childRef.value?.sayHelloFather()
})
</script>2.v-model
知道了如何將子組件中的數(shù)據(jù)如何傳遞給父組件,那如何在子組件中修改父組件中的數(shù)據(jù)呢,使用到了v-model + defineEmits的方法,首先在父組件中定義一個(gè)數(shù)據(jù),在其子組件標(biāo)簽上添加v-model:名稱 = '定義的數(shù)據(jù)名稱',在子組件中通過defineProps接收傳遞過來的數(shù)據(jù),然后使用defineEmits通知父組件去修改數(shù)據(jù),這邊我寫了一個(gè)小例子,代碼如下:
father.vue:
<template>
<div class="father">
<h2>父組件</h2>
<div>{{ content }}</div>
<Child v-model:content="content"></Child>
</div>
</template>
<script setup>
import { ref } from 'vue';
import Child from './Child.vue';
const content = ref('content')
</script>chlid.vue:
<template>
<div class="child">
<div>子組件</div>
<input type="text" :value="prop.content" @input="handleInput">
</div>
</template>
<script setup>
// 接收父組件傳遞過來的數(shù)據(jù)
const props = defineProps({
content: String
})
const emits = defineEmits(['update:content'])
function handleInput(e) {
const target = e.target.value
// 通知父組件修改數(shù)據(jù)
emits('update:content', target)
}
</script>這里例子的意思是,我修改表單的數(shù)據(jù)時(shí),父組件中對(duì)應(yīng)的數(shù)據(jù)也會(huì)跟著修改,這里有一點(diǎn)需要注意,在通知父組件修改數(shù)據(jù)的參數(shù),第一個(gè)參數(shù)是update:content,需要加上update,content需要跟父組件中的子組件標(biāo)簽上的v-model:content中content對(duì)應(yīng)。
在實(shí)際開發(fā)中父?jìng)髯雍妥觽鞲甘鞘褂妙l率最高的。
3.祖孫通信
當(dāng)需要進(jìn)行跨組件通信時(shí)可以使用到provide和inject(依賴注入)。
具體使用:在祖先組件中通過provide配置向后代組件提供數(shù)據(jù)。在后代組件中通過inject配置來聲明接收數(shù)據(jù)。
app.vue:
<template>
<div class="app">
<Father></Father>
</div>
</template>
<script setup>
import { provide, ref } from 'vue';
import Father from './components/Father.vue';
// state
const grandFatherData = ref('我是App.vue中的數(shù)據(jù),也就是你爺爺輩')
// function
function sayHello() {
console.log('hello');
}
provide('appData', { grandFatherData, sayHello })
</script>chlid.vue:
<template>
<div class="child">
<div>子組件</div>
<div>app.vue傳來的數(shù)據(jù):{{ data.grandFatherData }}</div>
</div>
</template>
<script setup>
import { inject, onMounted } from 'vue';
const data = inject('appData')
onMounted(()=>{
data.sayHello()
})
</script>這樣我們就可以獲取到祖先組件傳來的數(shù)據(jù)了
結(jié)語:
組件通信是Vue開發(fā)中的核心技能,掌握多種通信方式可以靈活應(yīng)對(duì)不同場(chǎng)景的需求。從簡(jiǎn)單的父子通信到復(fù)雜的跨層級(jí)通信,合理選擇方法能讓代碼更清晰、維護(hù)更方便。
到此這篇關(guān)于Vue3組件通信具體用法的文章就介紹到這了,更多相關(guān)Vue3組件通信內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue登錄頁面的動(dòng)態(tài)粒子背景插件實(shí)現(xiàn)
本文主要介紹了Vue登錄頁面的動(dòng)態(tài)粒子背景插件實(shí)現(xiàn),將登錄組件背景設(shè)置為 "粒子背景",具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
Vue源碼學(xué)習(xí)之?dāng)?shù)據(jù)初始化
這篇文章主要為大家介紹了Vue源碼學(xué)習(xí)之?dāng)?shù)據(jù)初始化實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
使用vue打包時(shí)vendor文件過大或者是app.js文件很大的問題
這篇文章主要介紹了使用vue打包時(shí)vendor文件過大或者是app.js文件很大問題的解決方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
解決vue更新路由router-view復(fù)用組件內(nèi)容不刷新的問題
今天小編就為大家分享一篇解決vue更新路由router-view復(fù)用組件內(nèi)容不刷新的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Vue3 數(shù)據(jù)總線的實(shí)現(xiàn)
在Vue3中,可以使用mitt實(shí)現(xiàn)事件總線,mitt是一個(gè)輕量級(jí)的事件發(fā)布/訂閱庫,核心功能包括發(fā)布事件、訂閱事件和取消訂閱,下面就來介紹一下如何使用,感興趣的可以了解一下2026-04-04
Vue3實(shí)現(xiàn)微信支付寶PC端支付的步驟(附實(shí)例代碼)
因?yàn)楣卷?xiàng)目涉及到充值功能所以做了支付寶、微信的支付功能,這篇文章主要介紹了Vue3實(shí)現(xiàn)微信支付寶PC端支付的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-09-09

