深入理解 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>
相关推荐
jay神17 小时前
基于团队模式的C程序设计课程辅助教学管理系统
java·spring boot·vue·web开发·管理系统
钛态18 小时前
前端TypeScript高级技巧:让你的代码更安全
前端·vue·react·web
吴声子夜歌2 天前
Vue3——路由管理
前端·vue·es6·vue-router
钛态2 天前
前端微前端架构:大项目的救命稻草还是自找麻烦?
前端·vue·react·web
钛态2 天前
前端趋势:别被时代抛弃
前端·vue·react·web
恶猫3 天前
网页自动化模拟操作时,模拟真实按键触发事件【终级方案】
前端·javascript·自动化·vue·网页模拟
无心使然云中漫步3 天前
Openlayers调用ArcGis地图服务之五 —— 要素识别(/identify)
前端·arcgis·vue·数据可视化
蜡台3 天前
H5入住浙里办App详细步骤
vue·uniapp·h5·浙政钉
呱牛do it4 天前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 7)
java·vue
呱牛do it6 天前
企业级门户网站设计与实现:基于SpringBoot + Vue3的全栈解决方案(Day 5)
java·vue