欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 名人名企 > Vue3组件通信的8种方式,完整源码带注释

Vue3组件通信的8种方式,完整源码带注释

2025/11/19 22:17:20 来源:https://blog.csdn.net/luochengcs000/article/details/144158217  浏览:    关键词:Vue3组件通信的8种方式,完整源码带注释

一文搞定Vue3通信的8种方式,源码请拿走

  • Vue3组件通信
    • 1、props父->子通信单向
      • 1.1 父组件
      • 1.2 子组件Child.vue
    • 2、自定义事件子->父通信单向
      • 2.1 父组件Father
      • 2.2 子组件Son.vue
    • 3、全局事件总线 任意组件<=>任意组件双向
      • 3.1 案例目标:兄弟节点通信
      • 3.2 mitt插件
      • 3.3 bus/bus.ts
      • 3.4 father.vue
      • 3.5 Child.vue
      • 3.6 OtherChild.vue
    • 4、v-model 父<=>子双向
      • 4.1 父组件
      • 4.2 子组件
    • 5、useAttrs函数 父<=>子双向
      • 5.1 父组件
      • 5.2 子组件使用useAttrs函数
    • 6、父组件使用ref 父->子单向
      • 6.1 父组件
      • 6.2 子组件
    • 7、子组件使用$parent 子->父单向
      • 7.1 父组件
      • 7.2 子组件
    • 8、provider和inject隔代双向
      • 8.1 父组件
      • 8.2 子组件
      • 8.3 孙子组件

Vue3组件通信

1、props父->子通信单向

props的数据是只读的

1.1 父组件

<template><div class="box"><h1>props:我是父组件</h1><hr/><!-- 父组件传递值--><Child :data="data"/></div>
</template><script setup lang="ts">import Child from './Child.vue'import {ref} from 'vue'let data = ref("我是你爹")
</script><style scoped>.box {width: 400px;heiht: 400px;background: green;}
</style>

1.2 子组件Child.vue

<template><div class="son"><h1>我是儿子</h1><!-- 读取父组件传递的值--><h2>{{money}}</h2></div>
</template>
<script setup lang="ts">// props是代理对象,代理中设置了money的值是readonly的,这里是定义属性let props = defineProps(['money'])
</script>
<style scoped>.son {width: 100px;height: 100px;background: red;}
</style>

2、自定义事件子->父通信单向

2.1 父组件Father

<template><div class="box"><h2>父组件接收到:{{p1}} -- {{p2}}</h2><Child @toFather="showData" /></div>
</template><script setup lang="ts">import Child from './Child.vue'import {ref} from 'vue'let p1 = ref()let p2 = ref()let showData = (param1,param2)=> {p1.value = param1p2.value = param2}
</script>
<style scoped>.box {width: 600px;height: 400px;background: green;}
</style>

2.2 子组件Son.vue

<template><div class="son"><button @click="sendToFather">传递参数给父组件</button></div>
</template><script setup lang="ts">let emits = defineEmits(['toFather'])let sendToFather = ()=> {// 调用父组件的方法toFatheremits('toFather','我是参数1','我是参数2')}
</script>
<style scoped>.son {width: 300px;height: 100px;background: red;}
</style>

3、全局事件总线 任意组件<=>任意组件双向

实现任意组件通信,需要使用插件,可以使用mit

3.1 案例目标:兄弟节点通信

1、接收数据方使用on绑定事件

2、发送数据方使用emit触发on绑定事件

3.2 mitt插件

1、安装mitt插件

在当前项目根目录下执行如下命令

$ npm install --save mitt

2、npmjs.com介绍的使用方式

import mitt from 'mitt'        // 导入const emitter = mitt()		   // 上一步导入的其实是一个函数// listen to an event   on方法监听一个事件,事件名称叫foo
emitter.on('foo', e => console.log('foo', e) )// listen to all events  监听所有的方法,使用*代替所有方法
emitter.on('*', (type, e) => console.log(type, e) )// fire an event         emit函数触发foo事件的执行
emitter.emit('foo', { a: 'b' })// clearing all events   清空所有的事件
emitter.all.clear()// working with handler references
function onFoo() {}
emitter.on('foo', onFoo)   // listen   绑定事件
emitter.off('foo', onFoo)  // unlisten   解绑事件

3.3 bus/bus.ts

import mitt from 'mitt'const $bus = 

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词