在 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>

深入理解 Vue 3 组件通信的更多相关文章

  1. Vue 兄弟组件通信(不使用Vuex)

    Vue 兄弟组件通信(不使用Vuex) 项目中,我们经常会遇到兄弟组件通信的情况.在大型项目中我们可以通过引入vuex轻松管理各组件之间通信问题,但在一些小型的项目中,我们就没有必要去引入vuex.下 ...

  2. Vue兄弟组件通信

    Vue兄弟组件通信之借助中央事件总线 下载链接:https://www.yinxiangit.com 其实要实现兄弟组件通信,就算是通过父子组件通信的方式也是可以达到的,如 子 ——>父——&g ...

  3. Vue父子组件通信(父级向子级传递数据、子级向父级传递数据、Vue父子组件存储到data数据的访问)

    Vue父子组件通信(父级向子级传递数据.子级向父级传递数据.Vue父子组件存储到data数据的访问) 一.父级向子级传递数据[Prop]: ● Prop:子组件在自身标签上,使用自定义的属性来接收外界 ...

  4. vue 父子组件通信

    算是初学vue,整理一下父子组件通信笔记. 父组件通过 prop 给子组件下发数据,子组件通过事件给父组件发送消息. 一.父组件向子组件下发数据: 1.在子组件中显式地用props选项声明它预期的数据 ...

  5. vue学习指南:第七篇(详细) - Vue的 组件通信

    Vue 的 父传子 子传父 一.父组件向子组件传值: 父传子 把需要的数据 传递给 子组件,以数据绑定(v-bind)的形式,传递到子组件内部,供子组件使用  缩写是(:) 1.创建子组件,在src/ ...

  6. 如何理解vue.js组件的作用域是独立的

    vue.js组件的作用域是独立,可以从以下三个方面理解: 1.父组件模板在父组件作用域内编译,父组件模板的数据用父组件内data数据:2.子组件模板在子组件作用域内编译,子组件模板的数据用子组件内da ...

  7. VUE简单组件通信

    [x] 1.prop组件通信 1.简单理解 2.多层嵌套 [x] 2.使用ref进行组件通信 [x] 3.$emit组件通信 1.prop组件通信 1.简单理解 有点类似于应式的感觉,我不管你要不要只 ...

  8. vue 父子组件通信props/emit

    props 1.父组件传递数据给子组件 父组件: <parent> <child :childMsg="msg"></child>//这里必须要 ...

  9. vue中组件通信

    组件的通信 1. 父子组件通信 案例:   //父子组件通信思路 // 1 将父组件的数据传给子组件 在子组件上自定义单项数据绑定 // 2 子组件用props 接受自定义的那个:号属性 Vue.co ...

  10. vue父子组件通信高级用法

    vue项目的一大亮点就是组件化.使用组件可以极大地提高项目中代码的复用率,减少代码量.但是使用组件最大的难点就是父子组件之间的通信. 子通信父 父组件 <template> <div ...

随机推荐

  1. Gitea 代码仓库平台

    引言 Gitea 是一个自己托管的 Git 服务程序.他和 GitHub,Bitbucket or Gitlab 等比较类似.它是从 Gogs 发展而来,不过它已经 Fork 并且命名为 Gitea. ...

  2. Linux之top命令分析

    第一行: top - 04:25:26 当前系统时间 up 3 min, 系统已经运行的时间(不间歇的运行) 1 user, 当前登录系统的用户数 load average: 0.01, 0.03, ...

  3. 安装图形化界面时候报错 Transaction check error: file /boot/efi/EFI/centos from install of fwupdate-efi-12-5.el7.centos.x86_64 conflicts with file from package grub2-common-1:2.02-0.65.el7.centos.2.noarch

    报错 Transaction check error:file /boot/efi/EFI/centos from install of fwupdate-efi-12-5.el7.centos.x8 ...

  4. Linux Shell命令提示样式修改

    对linux shell命令样式进行美化. 修改前的效果: 修改后的效果: 直接给出.bashrc脚本代码: 1 # ~/.bashrc: executed by bash(1) for non-lo ...

  5. 判断是不是ie浏览器 加上ie11

    var b_version = navigator.appVersion; var version = b_version.split(";"); var trim_Version ...

  6. 浏览器的同源策略 jsonp方法解决跨域

    // 浏览器的同源性         // 在 浏览器 处理请求时 默认执行的是 同源策略         // 也就是 浏览器 只 允许 同源的项目/请求 之间 获取响应体内容         // ...

  7. The sultion of P4959

    problem & blog 首先我们看到 \(x,y\) 有可能为负数,所以我们先把它旋转到第一象限. 然后我们发现如果 \(x_a \ge x_b\) 且 \(y_a \ge y_b\) ...

  8. 玩爆你的手机联系人--T9搜索

        自己研究了好几天联系人的T9搜索算法, 先分享出来给大家看看. 欢迎指教.如果有大神有更好的T9搜索算法, 那更好啊,大家一起研究研究,谢谢. 第一部分是比较简单的获取手机联系人. 获取联系人 ...

  9. 3个线程分别交替输出xyz字符,输出10遍

    一位群友分享的**公司面试题 3个线程分别交替输出xyz字符,输出10遍 public class XYZ implements Runnable { private static AtomicInt ...

  10. Linux日志搜索 grep

    1.关键字"或"的搜索, -E 不能少.grep -E "word1|word2|word3" file.txt满足任意条件(word1.word2和word3 ...