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

Vue2和Vue3组件间通信方式汇总(3)------$bus

 组件间通信方式是前端必不可少的知识点,前端开发经常会遇到组件间通信的情况,而且也是前端开发面试常问的知识点之一。接下来开始组件间通信方式第三弹------$bus,并讲讲分别在Vue2、Vue3中的表现。

Vue2+Vue3组件间通信方式汇总(1)------props

Vue2+Vue3组件间通信方式汇总(2)------$emit

一、全局总线$bus 原型链

归根结底就是在vm,vue原型链上注册一个名叫$bus 的对象,再把this,就是vm实例对象赋给$bus,其中$on $emit $off等就是全局可以读可写的变量,即可实现,相关组件、不相关组件之间数组地传递。

------Vue2 

main.js文件中,Vue实例下,往Vue原型链上注册属性:$bus

//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//关闭Vue的生产提示
Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App),beforeCreate() {Vue.prototype.$bus = this//注册全局事件总线}
})

其中一个组件:调用全局总线的$emit:

<template><div class="student"><h2>学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2><button @click="sendStudentName">把学生名给另一个组件</button></div>
</template><script>export default {name:'Student',data() {return {name:'张三',sex:'男',}},methods:{sendStudentName(){this.$bus.$emit('hello',this.name)}}}
</script><style scoped>.student{background-color: pink;padding: 5px;margin-top: 30px;}
</style>

 另一个组件:调用全局总线的$on:

<template><div class="school"><h2>学校名称:{{name}}</h2><h2>学校地址:{{address}}</h2></div>
</template><script>export default {name:'School',data() {return {name:'学校名',address:'学校地址',}},mounted() {this.$bus.$on('hello',(data) => { //绑定自定义事件hello,并留下回调函数console.log('我收到了'+data);})},beforeDestroy() {this.$bus.$off('hello')			},}
</script><style scoped>.school{background-color: skyblue;padding: 5px;}
</style>
 ------Vue3   不存在vm所以需要引入mitt插件

npm install mitt

在bus.ts文件中引入: 

import mitt from "mitt"
//mitt是一个函数,赋给命名为$bus的变量
const $bus=mitt();
//向外暴露这个变量
export default $bus

 其中一个组件:

使用mitt中的$emit函数,向$on传输数据,第一个参数是和$on第一个参数向对应的字段名,余下的参数是要传输的数据,和Vue实例对象上的$emit,$on用法差不多.

<template><div class="student"><h2>学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2><button @click="sendStudentName">把学生名给另一个组件</button></div>
</template><script setup lang="ts">
import ref from "vue"
import $bus from "./bus.ts"
let name=ref("张三")
let sex=ref("男")
let sendStudentName=(name.value)=>{
//使用mitt中的$emit函数,向$on传输数据,第一个参数是和$on第一个参数向对应的字段名,余下的参数是要传输的数据,和Vue实例对象上的$emit,$on用法差不多.$bus.$emit("hello",name.value)
}
</script><style scoped>.student{background-color: pink;padding: 5px;margin-top: 30px;}
</style>

 另一个组件:$on接收数据

<template><div class="student"><h2>学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2><button @click="sendStudentName">把学生名给另一个组件</button></div>
</template><script setup lang="ts">
import {ref,onMounted) from "vue"
import $bus from "./bus.ts"
let name=ref("张三")
let sex=ref("男")
onMounted(()=>{$bus.$on("hello",(data)=>{name.value=data})})</script><style scoped>.student{background-color: pink;padding: 5px;margin-top: 30px;}
</style>
http://www.lryc.cn/news/267328.html

相关文章:

  • PyTorch加载数据以及Tensorboard的使用
  • TensorFlow是什么
  • docker-compose 安装Sonar并集成gitlab
  • 支付平台在选择服务器租用时要注意什么?
  • IDEA2018升级2023,lombok插件不兼容导致get/set方法无法使用
  • 企业微信服务商代开发模式获取授权企业的客户信息
  • 库存管理方法有哪些
  • 数字化车间推动制造业生产创新
  • Linux的安装及管理程序
  • c语言-表达式求值
  • 小型洗衣机哪个牌子质量好?口碑最好的四款小型洗衣机推荐
  • springCould中的Ribbon-从小白开始【5】
  • 持续集成交付CICD:Jira 发布流水线
  • JuiceSSH结合内网穿透实现公网远程访问本地Linux虚拟机
  • 使用 pytest.ini 文件控制输出 log 日志
  • 【Spring】SpringBoot 配置文件
  • Koordinator 支持 K8s 与 YARN 混部,小红书在离线混部实践分享
  • 网游逆向分析与插件开发-游戏反调试功能的实现-项目需求与需求拆解
  • 阶段七-GitEE
  • Redis小记(1)
  • Flutter windows 环境配置
  • odoo17核心概念view5——ir_ui_view.py
  • 截断整型提升算数转换
  • 阿里云 ECS Docker、Docker Compose安装
  • LeetCode——1276. 不浪费原料的汉堡制作方案
  • 隆道吴树贵:生成式人工智能在招标采购中的应用
  • docker搭建kali及安装oneforall
  • 【MySQL】数据库之事务
  • AGV|RGV小车RFID传感器CNS-RFID-01/1S的RS232通讯联机方法
  • 【Python可视化系列】一文教会你绘制美观的热力图(理论+源码)