亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Vue3中使用混入(Mixin)的示例詳解

    Vue3中使用混入(Mixin)的示例詳解

    混入(Mixin)是?Vue?中一種代碼復用的模式,允許將組件的選項抽離為獨立模塊,下面就跟隨小編一起來深入了解下如何在Vue3中使用混入Mixin吧
    2025-03-03
  • Vue3使用全局函數(shù)或變量的2種常用方式代碼

    Vue3使用全局函數(shù)或變量的2種常用方式代碼

    在Vue3項目中需要頻繁使用某一個方法,配置到全局感覺會方便很多,這篇文章主要給大家介紹了關于Vue3使用全局函數(shù)或變量的2種常用方式,需要的朋友可以參考下
    2023-09-09
  • vue-cli創(chuàng)建的項目中的gitHooks原理解析

    vue-cli創(chuàng)建的項目中的gitHooks原理解析

    這篇文章主要介紹了vue-cli創(chuàng)建的項目中的gitHooks原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • 關于vue利用postcss-pxtorem進行移動端適配的問題

    關于vue利用postcss-pxtorem進行移動端適配的問題

    這篇文章主要介紹了關于vue利用postcss-pxtorem進行移動端適配的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-11-11
  • 利用vite創(chuàng)建vue3項目的全過程及一個小BUG詳解

    利用vite創(chuàng)建vue3項目的全過程及一個小BUG詳解

    Vite作為一個構建工具,提供了一種快速的方法來構建Vue應用,而Vue3?則是一個前端框架,提供了強大的功能來構建和管理前端項目,下面這篇文章主要給大家介紹了關于利用vite創(chuàng)建vue3項目的全過程及一個小BUG的相關資料,需要的朋友可以參考下
    2023-04-04
  • vue-router清除url地址欄路由參數(shù)的操作代碼

    vue-router清除url地址欄路由參數(shù)的操作代碼

    這篇文章主要介紹了vue-router清除url地址欄路由參數(shù),本文給大家介紹的非常詳細,需要的朋友可以參考下
    2015-11-11
  • vue開發(fā)樹形結構組件(組件遞歸)

    vue開發(fā)樹形結構組件(組件遞歸)

    這篇文章主要為大家詳細介紹了vue開發(fā)樹形結構組件的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • vue中echarts的用法及與elementui-select的協(xié)同綁定操作

    vue中echarts的用法及與elementui-select的協(xié)同綁定操作

    這篇文章主要介紹了vue中echarts的用法及與elementui-select的協(xié)同綁定操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • 關于Vue中keep-alive的作用及使用方法

    關于Vue中keep-alive的作用及使用方法

    keep-alive是Vue的內(nèi)置組件,當它包裹動態(tài)組件時,會緩存不活動的組件實例,該組件將不會銷毀,這篇文章主要介紹了關于Vue中keep-alive的作用是什么?怎么使用,需要的朋友可以參考下
    2023-04-04
  • vue使用formData時候傳遞參數(shù)是個空值的情況處理

    vue使用formData時候傳遞參數(shù)是個空值的情況處理

    這篇文章主要介紹了vue使用formData時候傳遞參數(shù)是個空值的情況處理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05

最新評論