vue3父子組件通信、兄弟組件實(shí)時(shí)通信方式
1.父組件向子組件通信
父組件:Father
<script setup>
import OneSon from "./oneSon.vue";
import { reactive } from "vue";
const state = reactive({
fatherData: "I am from Father.",
});
</script>
<template>
<p>I am Father.</p>
<OneSon :fatherDataName="state.fatherData"></OneSon>
</template>
<style scoped></style>子組件:OneSon
<script setup>
import { defineProps } from "vue";
defineProps({
fatherDataName: String,
});
</script>
<template>
<p>I am OneSon.</p>
<p>{{ fatherDataName }}</p>
</template>
<style scoped></style>效果:

2.子組件向父組件通信
子組件:OneSon
<script setup>
import { reactive, defineEmits } from "vue";
const state = reactive({
sonData: "I am from Son.",
});
const emit = defineEmits(["sonDataName"]);
const emitSonData = () => {
emit("sonDataName", state.sonData);
};
</script>
<template>
<p @click="emitSonData">I am OneSon.</p>
</template>
<style scoped></style>父組件:Father
<script setup>
import OneSon from "./oneSon.vue";
import { reactive } from "vue";
const state = reactive({
receive: "",
});
const getSonData = (value) => {
state.receive = value;
};
</script>
<template>
<p>I am Father.</p>
<OneSon @sonDataName="getSonData"></OneSon>
<p>{{ state.receive }}</p>
</template>
<style scoped></style>效果:

3.兄弟組件實(shí)時(shí)通信
子組件1:OneSon
<script setup>
import { reactive, defineEmits } from "vue";
const state = reactive({
sonData: true,
});
const emit = defineEmits(["sonDataName"]);
const emitSonData = () => {
emit("sonDataName", state.sonData);
};
</script>
<template>
<p @click="emitSonData">I am OneSon.</p>
</template>
<style scoped></style>子組件2:AnotherSon
<script setup>
import { reactive, defineExpose } from "vue";
const state = reactive({
display: false,
});
const showAnotherSon = (val) => {
state.display = val;
};
const hidden= () => {
state.display = false;
};
defineExpose({ showAnotherSon });
</script>
<template>
<p v-if="state.display" @click="hidden()">I am AnotherSon.</p>
</template>
<style scoped></style>父組件:Father
<script setup>
import OneSon from "./oneSon.vue";
import AnotherSon from "./anotherSon.vue";
import { ref } from "vue";
const anotherSon = ref(null);
const getSonData = (val) => {
anotherSon.value.showAnotherSon(val);
};
</script>
<template>
<p>I am Father.</p>
<OneSon @sonDataName="getSonData"></OneSon>
<AnotherSon ref="anotherSon"></AnotherSon>
</template>
<style scoped></style>效果:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue-cli創(chuàng)建的項(xiàng)目中的gitHooks原理解析
這篇文章主要介紹了vue-cli創(chuàng)建的項(xiàng)目中的gitHooks原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
關(guān)于vue利用postcss-pxtorem進(jìn)行移動(dòng)端適配的問(wèn)題
這篇文章主要介紹了關(guān)于vue利用postcss-pxtorem進(jìn)行移動(dòng)端適配的問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
利用vite創(chuàng)建vue3項(xiàng)目的全過(guò)程及一個(gè)小BUG詳解
Vite作為一個(gè)構(gòu)建工具,提供了一種快速的方法來(lái)構(gòu)建Vue應(yīng)用,而Vue3?則是一個(gè)前端框架,提供了強(qiáng)大的功能來(lái)構(gòu)建和管理前端項(xiàng)目,下面這篇文章主要給大家介紹了關(guān)于利用vite創(chuàng)建vue3項(xiàng)目的全過(guò)程及一個(gè)小BUG的相關(guān)資料,需要的朋友可以參考下2023-04-04
vue-router清除url地址欄路由參數(shù)的操作代碼
這篇文章主要介紹了vue-router清除url地址欄路由參數(shù),本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2015-11-11
vue開(kāi)發(fā)樹(shù)形結(jié)構(gòu)組件(組件遞歸)
這篇文章主要為大家詳細(xì)介紹了vue開(kāi)發(fā)樹(shù)形結(jié)構(gòu)組件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
vue中echarts的用法及與elementui-select的協(xié)同綁定操作
這篇文章主要介紹了vue中echarts的用法及與elementui-select的協(xié)同綁定操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
關(guān)于Vue中keep-alive的作用及使用方法
keep-alive是Vue的內(nèi)置組件,當(dāng)它包裹動(dòng)態(tài)組件時(shí),會(huì)緩存不活動(dòng)的組件實(shí)例,該組件將不會(huì)銷(xiāo)毀,這篇文章主要介紹了關(guān)于Vue中keep-alive的作用是什么?怎么使用,需要的朋友可以參考下2023-04-04
vue使用formData時(shí)候傳遞參數(shù)是個(gè)空值的情況處理
這篇文章主要介紹了vue使用formData時(shí)候傳遞參數(shù)是個(gè)空值的情況處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

