最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

深入理解?Vue?3實現(xiàn)組件通信的方法

 更新時間:2024年07月19日 08:57:33   作者:最小生成樹  
本文將介紹幾種常見的?Vue?3?組件通信方法,包括?props、emits、provide?和?inject、事件總線以及?Vuex?狀態(tài)管理,需要的朋友可以參考下

在 Vue 3 中,組件通信是一個關(guān)鍵的概念,它允許我們在組件之間傳遞數(shù)據(jù)和事件。本文將介紹幾種常見的 Vue 3 組件通信方法,包括 props、emits、provide 和 inject、事件總線以及 Vuex 狀態(tài)管理。

1. 使用 props 和 emits 進行父子組件通信

props 傳遞數(shù)據(jù)

props 是父組件向子組件傳遞數(shù)據(jù)的一種機制。在子組件中,通過定義 props 屬性來接收父組件傳遞的數(shù)據(jù)。

父組件 (ParentComponent.vue):

<template>
  <ChildComponent :message="parentMessage" />
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      parentMessage: 'Hello from Parent Component!'
    };
  }
};
</script>

子組件 (ChildComponent.vue):

<template>
  <div>{{ message }}</div>
</template>
<script>
export default {
  props: {
    message: {
      type: String,
      required: true
    }
  }
};
</script>

emits 傳遞事件

子組件可以通過 $emit 方法向父組件發(fā)送事件,從而實現(xiàn)從子組件向父組件傳遞信息。

子組件 (ChildComponent.vue):

<template>
  <button @click="sendMessage">Send Message</button>
</template>
<script>
export default {
  emits: ['messageSent'],
  methods: {
    sendMessage() {
      this.$emit('messageSent', 'Hello from Child Component!');
    }
  }
};
</script>

父組件 (ParentComponent.vue):

<template>
  <ChildComponent @messageSent="handleMessage" />
</template>
<script>
import ChildComponent from './ChildComponent.vue';
export default {
  components: {
    ChildComponent
  },
  methods: {
    handleMessage(message) {
      console.log(message);
    }
  }
};
</script>

2. 使用 provide 和 inject 進行祖孫組件通信

provide 和 inject 允許祖父組件和孫組件之間進行通信,而不需要通過中間的父組件傳遞數(shù)據(jù)。

祖父組件 (GrandparentComponent.vue):

<template>
  <ParentComponent />
</template>
<script>
import ParentComponent from './ParentComponent.vue';
export default {
  components: {
    ParentComponent
  },
  provide() {
    return {
      grandparentMessage: 'Hello from Grandparent Component!'
    };
  }
};
</script>

孫組件 (GrandchildComponent.vue):

<template>
  <div>{{ grandparentMessage }}</div>
</template>
<script>
export default {
  inject: ['grandparentMessage']
};
</script>

3. 使用事件總線進行兄弟組件通信

事件總線是一種常見的用于兄弟組件通信的方法,通常使用 Vue 實例作為事件總線。

事件總線 (eventBus.js):

import { reactive } from 'vue';
const eventBus = reactive({});
export default eventBus;

組件 A (ComponentA.vue):

<template>
  <button @click="sendMessage">Send Message to Component B</button>
</template>
<script>
import eventBus from './eventBus.js';
export default {
  methods: {
    sendMessage() {
      eventBus.message = 'Hello from Component A!';
    }
  }
};
</script>

組件 B (ComponentB.vue):

<template>
  <div>{{ message }}</div>
</template>
<script>
import { reactive, toRefs } from 'vue';
import eventBus from './eventBus.js';
export default {
  setup() {
    const state = reactive({
      message: ''
    });
    state.message = eventBus.message;
    return {
      ...toRefs(state)
    };
  }
};
</script>

到此這篇關(guān)于深入理解 Vue 3 組件通信的文章就介紹到這了,更多相關(guān)Vue 3 組件通信內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue實現(xiàn)點擊按鈕“查看詳情”彈窗展示詳情列表操作

    vue實現(xiàn)點擊按鈕“查看詳情”彈窗展示詳情列表操作

    這篇文章主要介紹了vue實現(xiàn)點擊按鈕“查看詳情”彈窗展示詳情列表操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • vue3結(jié)合typescript中使用class封裝axios

    vue3結(jié)合typescript中使用class封裝axios

    這篇文章主要為大家介紹了vue3結(jié)合typescript中使用class封裝axios實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • vue動態(tài)綁定v-model屬性名方式

    vue動態(tài)綁定v-model屬性名方式

    這篇文章主要介紹了vue動態(tài)綁定v-model屬性名方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue 虛擬DOM快速入門

    vue 虛擬DOM快速入門

    這篇文章主要介紹了vue 虛擬DOM的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-04-04
  • Vue實現(xiàn)拖放排序功能的實例代碼

    Vue實現(xiàn)拖放排序功能的實例代碼

    本文通過實例代碼給大家介紹了Vue中實現(xiàn)拖放排序功能,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • VS編譯器中引用Vue3框架的實現(xiàn)步驟

    VS編譯器中引用Vue3框架的實現(xiàn)步驟

    本文主要介紹了VS編譯器中引用Vue3框架的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • Element-Plus之el-col與el-row快速布局

    Element-Plus之el-col與el-row快速布局

    el-col是el-row的子元素,下面這篇文章主要給大家介紹了關(guān)于Element-Plus之el-col與el-row快速布局的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • 解決vue A對象賦值給B對象,修改B屬性會影響到A的問題

    解決vue A對象賦值給B對象,修改B屬性會影響到A的問題

    今天小編就為大家分享一篇解決vue A對象賦值給B對象,修改B屬性會影響到A的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vue3+ts+EsLint+Prettier規(guī)范代碼的方法實現(xiàn)

    vue3+ts+EsLint+Prettier規(guī)范代碼的方法實現(xiàn)

    本文主要介紹在Vue3中使用TypeScript做開發(fā)時,如何安裝與配置EsLint和Prettier,以提高編碼規(guī)范。感興趣的可以了解一下
    2021-10-10
  • 淺談Vue數(shù)據(jù)綁定的原理

    淺談Vue數(shù)據(jù)綁定的原理

    本篇文章主要介紹了淺談Vue數(shù)據(jù)綁定的原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01

最新評論

兴城市| 漳平市| 沛县| 民县| 连云港市| 资溪县| 凉城县| 宾阳县| 罗江县| 鄱阳县| 吕梁市| 泰来县| 丹东市| 永清县| 沂源县| 泾源县| 嵩明县| 高台县| 平安县| 龙门县| 怀仁县| 绥江县| 汽车| 灯塔市| 桃江县| 封丘县| 甘孜| 澎湖县| 房产| 香港| 古浪县| 监利县| 濉溪县| 彭阳县| 苏州市| 吴堡县| 包头市| 水富县| 长白| 沙雅县| 大余县|