深入理解 Vue 3 组件通信

在 Vue 3 中,组件通信是一个关键的概念,它允许我们在组件之间传递数据和事件。本文将介绍几种常见的 Vue 3 组件通信方法,包括 propsemitsprovideinject、事件总线以及 Vuex 状态管理。

1. 使用 propsemits 进行父子组件通信

props 传递数据

props 是父组件向子组件传递数据的一种机制。在子组件中,通过定义 props 属性来接收父组件传递的数据。

父组件 (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 方法向父组件发送事件,从而实现从子组件向父组件传递信息。

子组件 (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. 使用 provideinject 进行祖孙组件通信

provideinject 允许祖父组件和孙组件之间进行通信,而不需要通过中间的父组件传递数据。

祖父组件 (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>
相关推荐
FindYou.13 小时前
基于mdEditor实现数据的存储和回显(导出pdf&表情包&目录)
javascript·vue
PD我是你的真爱粉2 天前
Vue3核心语法回顾与Composition深入
前端框架·vue
code袁2 天前
基于Springboot+Vue的家教小程序的设计与实现
vue.js·spring boot·小程序·vue·家教小程序
是梦终空3 天前
计算机毕业设计266—基于Springboot+Vue3的共享单车管理系统(源代码+数据库)
数据库·spring boot·vue·课程设计·计算机毕业设计·源代码·共享单车系统
是梦终空3 天前
计算机毕业设计267—基于Springboot+vue3+小程序的医院挂号系统(源代码+数据库)
spring boot·小程序·vue·毕业设计·课程设计·医院挂号系统·源代码
云游云记4 天前
vue2 vue3 uniapp (微信小程序) v-model双向绑定
微信小程序·uni-app·vue
沐墨染4 天前
黑词分析与可疑对话挖掘组件的设计与实现
前端·elementui·数据挖掘·数据分析·vue·visual studio code
集成显卡5 天前
前端视频播放方案选型:主流 Web 播放器对比 + Vue3 实战
前端·vue·音视频
克里斯蒂亚诺·罗纳尔达5 天前
vue页面加载时间过长优化
vue
草根大哥5 天前
AI编程实践-homex物业管理平台(Go + Vue3 + MySQL 多租户落地)
mysql·golang·vue·ai编程·gin·物业管理系统·多租户