Q1:在vue3中,取消了bus,有什么替代品?如果手动实现一个bus,如何实现? 实现EventBus整个EventBus主要部分是分为三个部分。消息中心,订阅事件方法,发布消息方法。 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748// 构造EventBusfunction EventBusClass() { this.msgQueues = {}}EventBusClass.prototype = { // 将消息保存到当前的消息队列中 on: function(msgName, func) { if (this.msgQueues.hasOwnProperty(msgName)) { if (typeof this.msgQueues[msgName] === 'function') { this.msgQueues[msgName] = [this.msgQueues[msgName], func] } else { this.msgQueues[msgName] = [...this.msgQueues[msgName], func] } } else { this.msgQueues[msgName] = func; } }, // 消息队列中仅保存一个消息 one: function(msgName, func) { // 无需检查msgName是否存在 this.msgQueues[msgName] = func; }, // 发送消息 emit: function(msgName, msg) { if (!this.msgQueues.hasOwnProperty(msgName)) { return } if (typeof this.msgQueues[msgName] === 'function') { this.msgQueues[msgName](msg) } else { this.msgQueues[msgName].map((fn) => { fn(msg) }) } }, // 移除消息 off: function(msgName) { if (!this.msgQueues.hasOwnProperty(msgName)) { return } delete this.msgQueues[msgName] }}// 将EventBus放到window对象中const EventBus = new EventBusClass()window.EventBus = EventBus 使用EventBus1234567891011121314151617// 订阅消息function subscribe() { EventBus.on('first-event', function(msg) { alert(`订阅的消息是:${msg}`); });}// 发送消息function emit() { const msgInput = document.getElementById("msgInputId") EventBus.emit('first-event', msgInput.value)}// 移除消息function off(msgName) { EventBus.off(msgName)} 替代品:mitt