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

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

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

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

1. 使用 props 和 emits 進(jìn)行父子組件通信

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

props 是父組件向子組件傳遞數(shù)據(jù)的一種機(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ā)送事件,從而實(shí)現(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 進(jìn)行祖孫組件通信

provide 和 inject 允許祖父組件和孫組件之間進(jìn)行通信,而不需要通過中間的父組件傳遞數(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. 使用事件總線進(jìn)行兄弟組件通信

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

事件總線 (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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

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

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

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

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

    vue 虛擬DOM快速入門

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

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

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

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

    本文主要介紹了VS編譯器中引用Vue3框架的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • 解決vue A對(duì)象賦值給B對(duì)象,修改B屬性會(huì)影響到A的問題

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

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

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

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

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

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

最新評(píng)論

双鸭山市| 新竹县| 宕昌县| 旬邑县| 突泉县| 新泰市| 田东县| 龙山县| 正安县| 蒙山县| 如皋市| 竹北市| 兴业县| 江源县| 乐东| 绿春县| 永和县| 聂荣县| 高陵县| 石城县| 铁岭市| 湖北省| 密云县| 雷山县| 莆田市| 石泉县| 铜山县| 横峰县| 岗巴县| 承德市| 西平县| 资源县| 德庆县| 庆城县| 怀柔区| 武胜县| 宁陕县| 井研县| 望都县| 海淀区| 金阳县|