当前位置: 首页 > article >正文

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 = param1
        p2.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 = ()=> {
        // 调用父组件的方法toFather
        emits('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 = 

http://www.kler.cn/a/416676.html

相关文章:

  • 图解:XSS攻击原理与安全过滤
  • ip租期到了
  • Qt关于padding设置不起作用的的解决办法
  • 【C++】泛型算法(三):定制操作
  • Android 图形系统之三:SurfaceControl
  • aws rds-mysql不支持性能详情监控
  • Jackson库中@JsonAlias和@JsonProperty的使用和区别
  • 挑战用React封装100个组件【002】
  • 2025年人工智能,自动化与机械工程国际学术会议(AIAME2025)
  • 颜色分类
  • CPU性能优化-CPU特性
  • 求100之内的素数-多语言
  • 分布式锁的实现原理
  • HarmonyOS-中级(一)
  • 嵌入式QT学习第4天:Qt 信号与槽
  • 无人机数据处理系统:原理与核心系统
  • 12 设计模式之工厂方法模式
  • 华为OD机试真题-数组组成的最小数-2024年OD统一考试(E卷)
  • 管家婆工贸ERP BR042.存货自定义项虚拟库存状况表
  • mysql--二进制安装编译安装yum安装