Vue2-Vue3组件间通信-EventBus方式-函数封装

chatgpt/2023/9/24 2:36:48

Vue3中采用EventBus方式进行组件间通信与Vue2有一定区别

1.创建EventBus

在Vue2中,我们可以在main.js中创建一个全局的EventBus,代码如下:

// EventBus.js
import Vue from 'vue'
const EventBus = new Vue()
export default EventBus// main.js
import EventBus from './EventBus'
Vue.prototype.$bus = EventBus

在Vue3中,我们需要使用createApp来创建Vue实例,并使用provide和inject来创建全局的EventBus,代码如下:

// EventBus.js
import { createApp } from 'vue'
const app = createApp({})
const EventBus = app.provide('EventBus', new app())
export default EventBus// main.js
import EventBus from './EventBus'
createApp(App).use(EventBus).mount('#app')-----------------
// 在 main.js 或入口文件中
import { createApp, provide } from 'vue';
import App from './App.vue';const app = createApp(App);
const eventBus = createApp({});// 将 eventBus 实例作为提供者提供给子组件
app.provide('eventBus', eventBus);app.mount('#app');

2.使用EventBus进行通信

在Vue2中,我们可以通过$emit和$on方法来进行通信,代码如下:

// 发送通信
this.$emit('event', data)// 接收通信
this.$on('event', (data) => {console.log(data)
})// 销毁
this.$off('event')

在Vue3中,我们可以通过$emit和$on方法来进行通信,但是需要在组件中使用AppContext来获取EventBus,代码如下:

// 发送通信
const app = getCurrentInstance().appContext.app
app.config.globalProperties.$EventBus.emit('event', data)// 接收通信
const app = getCurrentInstance().appContext.app
app.config.globalProperties.$EventBus.on('event', (data) => {console.log(data)
})//  销毁
const app = getCurrentInstance().appContext.app
app.config.globalProperties.$EventBus.off('event')-------------------
import { inject } from 'vue';export default {// ...created() {const eventBus = inject('eventBus');// 使用 eventBus 进行事件的发布和订阅},// ...
}

总的来说,Vue3与Vue2在EventBus方式上的区别不大,只是在创建全局EventBus的方式上有所不同,但是使用起来差异较大,需要根据实际情况进行选择。


封装EventBus.js

类方式 

class Event {constructor() {this.queue = {};this.onceQueue = {};}$on(name, callback) {this.queue[name] = this.queue[name] || [];this.queue[name].push(callback);}$once(name, callback) {this.onceQueue[name] = this.onceQueue[name] || [];this.onceQueue[name].push(callback);}$off(name, callback) {if (callback) {if (this.queue[name]) {for (var i = 0; i < this.queue[name].length; i++) {if (this.queue[name][i] === callback) {this.queue[name].splice(i, 1);break;}}}} else {delete this.queue[name];}}$emit(name, data) {if (this.queue[name]) {this.queue[name].forEach(function (callback) {callback(data);});}if (this.onceQueue[name]) {this.onceQueue[name].forEach(function (callback) {callback(data);});delete this.onceQueue[name];}}
}export default new Event();

使用 

import Bus from '@/utils/EventBus';
Bus.$on('test', (data) => {})
Bus.$emit('close')
beforeUnmount() {
    Bus.$off('test', fun)
}

构造函数方式

function E() { }// 函数E的原型对象
E.prototype = {// on方法:接受订阅名,订阅函数,上下文对象on: function (name, callback, context) {// 初始化e仓库var e = this.e || (this.e = {})// 收集订阅函数// 包装为对象,收集订阅函数与上下文对象; (e[name] || (e[name] = [])).push({fn: callback,context})// 返回实例对象return this},// once函数:接收订阅名,订阅函数,上下文对象// 与on的区别是:once函数收集只执行一遍的订阅函数once: function (name, callback, context) {let self = this// 包装对象,用于自定义执行逻辑(删除操作)function listener() {self.off(name, listener)callback.apply(context, arguments)}// 保存原始函数listener._ = callback// 使用on收集自定义后的函数// 执行on方法会返回this,所以once函数内不需要返回thisreturn this.on(name, listener, context)},// emit方法用于触发订阅函数:接收订阅名称emit: function (name) {// 收集参数let args = [].slice.call(arguments, 1)// 收集订阅函数数组let events = ((this.e || (this.e = {}))[name] || []).slice()let i = 0let len = events.length// 循环执行订阅函数for (; i < len; i++) {// 使用apply调用函数并绑定thisevents[i].fn.apply(events[i].context, args)}// 返回this实例return this},// off用于删除订阅函数:接收订阅名和订阅函数off: function (name, callback) {let e = this.e || (this.e = {})// 获取订阅名称对应的数组let events = e[name]let liveEvents = []// 处理函数数组&传入的订阅函数是否都存在?if (events && callback) {// 循环遍历,过滤操作for (let i = 0, len = events.length; i < len; i++) {// 判断数组中的订阅函数是否与传入的订阅函数相等?// 使用once创建的函数取_属性中的原始函数进行对比if (events[i].fn !== callback && events[i].fn._ !== callback) {liveEvents.push(events[i])}}}// 重置订阅名结果数组(liveEvents.length) ? e[name] = liveEvents : delete e[name]// 返回实例thisreturn this}
}export default {$on: (...args) => E.prototype.on(...args),$once: (...args) => E.prototype.once(...args),$off: (...args) => E.prototype.off(...args),$emit: (...args) => E.prototype.emit(...args)
}

使用 

import Bus from "@/utils/EventBus2";
Bus.$on('test', (data) => {})
Bus.$emit('close')
beforeUnmount() {
    Bus.$off('test', fun)
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.exyb.cn/news/show-5314052.html

如若内容造成侵权/违法违规/事实不符,请联系郑州代理记账网进行投诉反馈,一经查实,立即删除!

相关文章

排序算法(冒泡排序、选择排序、插入排序、希尔排序、堆排序、快速排序、归并排序、计数排序)

&#x1f355;博客主页&#xff1a;️自信不孤单 &#x1f36c;文章专栏&#xff1a;数据结构与算法 &#x1f35a;代码仓库&#xff1a;破浪晓梦 &#x1f36d;欢迎关注&#xff1a;欢迎大家点赞收藏关注 文章目录 &#x1f353;冒泡排序概念算法步骤动图演示代码 &#x1f34…

防御第三天

1.总结当堂NAT与双机热备原理&#xff0c;形成思维导图 2.完成课堂NAT与双机热备实验 fw1: <USG6000V1>sy [USG6000V1]int g0/0/0 [USG6000V1-GigabitEthernet0/0/0]ip add 192.168.18.2 24 [USG6000V1-GigabitEthernet0/0/0]service-manage all permit (地址无所谓&…

RabbitMQ 教程 | 第2章 RabbitMQ 入门

&#x1f468;&#x1f3fb;‍&#x1f4bb; 热爱摄影的程序员 &#x1f468;&#x1f3fb;‍&#x1f3a8; 喜欢编码的设计师 &#x1f9d5;&#x1f3fb; 擅长设计的剪辑师 &#x1f9d1;&#x1f3fb;‍&#x1f3eb; 一位高冷无情的编码爱好者 大家好&#xff0c;我是 DevO…

​小程序 WxValidate.js ​再次封装

util.js // 合并验证规则和提示信息 const filterRules (objectItem) > {let rules {}, messages {};for (let key in objectItem) {rules[key] objectItem[key].rulesmessages[key] objectItem[key].message}return { rules, messages } }module.exports {filterRule…

Redis学习路线(7)—— Redis的实现MQ

一、Redis的消息队列 list结构&#xff1a; 基于List结构模拟MQPubSub&#xff1a; 基本的 P2P 模式的 MQ模型Stream&#xff1a; 比较完善的MQ模型 1、List模拟MQ&#xff1a; 通过LPUSH推送消息&#xff0c;RPOP接收消息&#xff0c;若想要阻塞队列的效果&#xff0c;可以通…

【面试】某公司记录一次面试题

文章目录 框架类1. Spring boot与 spring 架相比&#xff0c;好在哪里?2. Spring boot以及 Spring MVC 常用注解(如requestingMapping&#xff0c;responseBody 等)3. 常用的java 设计模式&#xff0c;spring 中用到哪些设计模式4. SpringIOC是什么&#xff0c;如何理解5. AOP…

XGBoost的基础思想与实现

目录 1. XGBoost VS 梯度提升树 1.1 XGBoost实现精确性与复杂度之间的平衡 1.2 XGBoost极大程度地降低模型复杂度、提升模型运行效率 1.3 保留了部分与梯度提升树类似的属性 2. XGBoost回归的sklearnAPI实现 2.1 sklearn API 实现回归 2.2 sklearn API 实现分类 3. XGBo…

AD21 PCB设计的高级应用(五)模块复用的操作

&#xff08;五&#xff09;模块复用的操作 1.利用Room实现相同模块复用2.复制粘贴功能实现模块复用 1.利用Room实现相同模块复用 本小节介绍 Altium Designer 两种常用模块复用方法:一种是利用 Room 实现相同模块复用,另一种是利用复制粘贴功能实现。 1.利用Room实现相同模块…
推荐文章