深入理解 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>
相关推荐
碧水澜庭4 小时前
头条【vue+fastapi 】全栈教学项目系列之《02_双系统零基础环境搭建手册》
vue·fastapi
DsirNg2 天前
从 `@wheel.prevent.stop` 到事件捕获:一次讲清浏览器事件流与 Vue 事件修饰符
javascript·vue·事件修饰符·事件冒泡·dom 事件·事件捕获·wheel
其美杰布-富贵-李2 天前
Vue 3 模板语法速通:彻底理解 `:`、`@`、`#`、`v-if`、`v-for` 与 `v-model`
vue
DsirNg3 天前
基于拓扑排序的画布自动布局:用最长上游路径决定节点列级
vue·流程图·拓扑排序·状态管理·有向图·自动布局·最长路径
way_hj4 天前
Flask笔记(2)快速web项目
笔记·flask·vue·web
Sterting4 天前
初识Vue3与开发环境搭建
前端·vue
sugar__salt5 天前
跟着 Demo 学 Pinia:两种仓库写法 + 完整 TodoList 复现
前端·javascript·vue.js·前端框架·vue
弹简特5 天前
【Vue3速成】09-Element Plus 核心组件实战与原理解析
vue·element-plus
钛态5 天前
AI 辅助:前端框架反模式:过度封装、状态滥用与副作用失控
前端·vue·react·web
by__csdn5 天前
Vue vs React vs Angular:前端框架终极对决
前端·vue.js·react.js·前端框架·vue·react·angular