vue3父子組件通信、兄弟組件實時通信方式
更新時間:2023年06月08日 09:48:00 作者:acheding
這篇文章主要介紹了vue3父子組件通信、兄弟組件實時通信方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
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.兄弟組件實時通信
子組件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>
效果:
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue-cli創(chuàng)建的項目中的gitHooks原理解析
這篇文章主要介紹了vue-cli創(chuàng)建的項目中的gitHooks原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02關于vue利用postcss-pxtorem進行移動端適配的問題
這篇文章主要介紹了關于vue利用postcss-pxtorem進行移動端適配的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11利用vite創(chuàng)建vue3項目的全過程及一個小BUG詳解
Vite作為一個構建工具,提供了一種快速的方法來構建Vue應用,而Vue3?則是一個前端框架,提供了強大的功能來構建和管理前端項目,下面這篇文章主要給大家介紹了關于利用vite創(chuàng)建vue3項目的全過程及一個小BUG的相關資料,需要的朋友可以參考下2023-04-04vue-router清除url地址欄路由參數(shù)的操作代碼
這篇文章主要介紹了vue-router清除url地址欄路由參數(shù),本文給大家介紹的非常詳細,需要的朋友可以參考下2015-11-11vue中echarts的用法及與elementui-select的協(xié)同綁定操作
這篇文章主要介紹了vue中echarts的用法及與elementui-select的協(xié)同綁定操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11vue使用formData時候傳遞參數(shù)是個空值的情況處理
這篇文章主要介紹了vue使用formData時候傳遞參數(shù)是個空值的情況處理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05