Vue3組件通信的具體用法實(shí)例
前言:
在組件化開發(fā)中,需要將頁面抽離成組件的形式,抽離之后就涉及到了組件中數(shù)據(jù)傳遞,可分為:父傳子(props)、子傳父(emits)、祖孫通信(provide和inject)、兄弟通信、全局通訊(pinia)。這次我就以博客的形式復(fù)習(xí)一下前三種通訊,想了解pinia可點(diǎn)擊看我前面寫的博客。
1.父傳子
首先需要在父組件中的子組件標(biāo)簽中添加自定義屬性,將需要傳遞的值放如自定義屬性中,在子組件中通過defineProps這個方法獲取父組件傳遞過來的值。具體方法如下:
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ù)據(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的方法,首先在父組件中定義一個數(shù)據(jù),在其子組件標(biāo)簽上添加v-model:名稱 = '定義的數(shù)據(jù)名稱',在子組件中通過defineProps接收傳遞過來的數(shù)據(jù),然后使用defineEmits通知父組件去修改數(shù)據(jù),這邊我寫了一個小例子,代碼如下:
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ù)時,父組件中對應(yīng)的數(shù)據(jù)也會跟著修改,這里有一點(diǎn)需要注意,在通知父組件修改數(shù)據(jù)的參數(shù),第一個參數(shù)是update:content,需要加上update,content需要跟父組件中的子組件標(biāo)簽上的v-model:content中content對應(yīng)。
在實(shí)際開發(fā)中父傳子和子傳父是使用頻率最高的。
3.祖孫通信
當(dāng)需要進(jìn)行跨組件通信時可以使用到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)對不同場景的需求。從簡單的父子通信到復(fù)雜的跨層級通信,合理選擇方法能讓代碼更清晰、維護(hù)更方便。
到此這篇關(guān)于Vue3組件通信具體用法的文章就介紹到這了,更多相關(guān)Vue3組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容
這篇文章主要介紹了vue3 el-form-item如何自定義label標(biāo)簽內(nèi)容問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue 多選框所選數(shù)量動態(tài)變換Box的高度
在Web開發(fā)中,使用Vue.js框架可以通過ref屬性、v-model指令和計算屬性等特性實(shí)現(xiàn)元素高度的動態(tài)調(diào)整,文章詳細(xì)介紹了如何利用Vue的功能根據(jù)多選框的選擇數(shù)量動態(tài)改變元素的高度,并通過多個示例展示其應(yīng)用2024-09-09
vue級聯(lián)選擇器的getCheckedNodes使用方式
這篇文章主要介紹了vue級聯(lián)選擇器的getCheckedNodes使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
關(guān)于keep-alive路由多級嵌套不生效的解決方案
本文主要介紹了關(guān)于keep-alive路由多級嵌套不生效的解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
vue項(xiàng)目或網(wǎng)頁上實(shí)現(xiàn)文字轉(zhuǎn)換成語音播放功能
這篇文章主要介紹了在vue項(xiàng)目或網(wǎng)頁上實(shí)現(xiàn)文字轉(zhuǎn)換成語音,需要的朋友可以參考下2020-06-06

