vue.js 3活动巴士
如何在VUE 3中创建活动总线?
在VUE 2中,它是:
export const bus = new Vue();
bus.$on(...)
bus.$emit(...)
在VUE 3中, vue
不再是构造函数, vue.createapp({});
返回一个没有的对象$ on
和 $ emit
方法。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如官方 docs 您可以使用<< a href =“ https://github.com/developit/mitt” rel =“ noreferrer”> mitt 库要在组件之间调度事件,让我们假设我们有一个侧边栏和
标题
,其中包含一个关闭/打开侧边栏的按钮,我们需要该按钮来切换侧栏组件内的某些属性:在main.js中导入该库并创建一个发射器的实例,并将其定义为a 全局属性:
安装:
用法:
在标题中发射
toggle-sidebar
带有有效载荷的事件:在侧边栏中接收事件,并带有有效载荷:
对于使用构图API的人,他们可以使用
emitter
如下:创建一个文件src/composables/useemitter.js
,从那里您可以使用
use -empitter
,就像您一样使用UserOuter
:使用构图API
您也可以从新的组成API中受益,并定义一个可组合的事件总线:
eventbus.js
在组件A中:
在组件B中:
As suggested in official docs you could use mitt library to dispatch events between components, let suppose that we have a sidebar and
header
which contains a button that close/open the sidebar and we need that button to toggle some property inside the sidebar component :in main.js import that library and create an instance of that emitter and define as a global property:
Installation :
Usage :
in header emit the
toggle-sidebar
event with some payload :In sidebar receive the event with the payload:
For those using composition api they could use
emitter
as follows :Create a file src/composables/useEmitter.js
And from there on you can use
useEmitter
just like you would withuseRouter
:Using the composition API
You could also take benefit from the new composition API and define a composable event bus :
eventBus.js
in component A do:
in component B :
在vue.js的版本3上,您可以使用第三方库,也可以使用发布者 - bubscriber(pubsub概念)编程模式中编写的功能。
event.js
index.js
On version 3 of Vue.js, you can use either a third-party library, or use the functionality written in the publisher-subscriber(PubSub concept) programming pattern.
event.js
index.js
EventBus类文件的内容:
在项目中使用,发射事件:
收听事件:
Content of EventBus class file:
usage in project, emit event:
listen event:
我只想在这里提到您也可以使用 useeventbus 由 vueuse 。
这是打字稿的一个示例,因此使用注射键。
I just want to mention here that you can also use useEventBus defined by VueUse.
Here is one example for TypeScript so using an injection key.
我已经改编了另一个答案具有与VUE实例的等效接口这不需要更改消耗代码。
此版本还支持
$ off
方法,第一个参数是事件名称的 array 。它还避免了$ off
方法中的一个问题,即删除多个事件侦听器实际上会删除错误的侦听器,因为迭代沿向前的方向上的数组迭代,同时也从中删除了项目。event-bus.js
:old-event-bus.js
:example.js
:I’ve adapted another answer to have an equivalent interface to a Vue instance so that the utility works as a drop-in replacement that doesn’t require changes in the consuming code.
This version also supports the
$off
method with the first argument being an array of event names. It also avoids an issue in the$off
method were de-registering multiple event listeners would actually delete a wrong one due to iterating over the array in forwards direction while also deleting items from it.event-bus.js
:old-event-bus.js
:example.js
:这是Boussadjra Brahim的Reption Api的改进版本。
这种方法使您可以使用有效载荷多次触发事件。
请注意,如果几次触发同一事件时, counter 仅用于触发更改,则使用有效载荷或有效载荷相同。
还要谨慎使用SSR,在这种情况下,总线变量将共享,因此您最终会得到交叉要求的状态污染。为了避免这种情况,您可以将事件总线作为插件实现,并在需要时将其曝光。像这样,将为每个请求初始化总线变量。
useeventbus.js
PublisherComponent.vue
subscribercomponent.vue
Here is improved version of Boussadjra Brahim's answer with composition API.
This approach allows you to trigger an event multiple times with payload or without it.
Note, that counter is used just to trigger a change if no payload or payload is the same when you trigger same event several times.
Also be careful with SSR, in this case the bus variable will be shared, so you end up with cross-request state pollution. To avoid this you may implement event bus as a plugin and if needed expose it with composable. Like this the bus variable will be initialized for each request.
useEventBus.js
PublisherComponent.vue
SubscriberComponent.vue
不确定为什么VUE3文档不会引起此提示,但我们可以使用窗口上的事件并在我们所需的VUE3组件中捕获事件:
让我们考虑您想要能够要触发VUE3项目中任何地方的模态(
app.vue
),而您的 源组件(
app.vue
)可以: .vue )可以在安装时开始聆听该事件:Not sure why vue3 documentation doesn't hint this but we can use javascript custom events on the window and capture the event in our desired vue3 component:
Let's consider you want to be able to trigger a modal from anywhere in your vue3 project (
App.vue
) and your source component (App.vue
) can have:And your modal component (
Modal.vue
) can start listening to that event whenever mounted:使用 https://www.npmjs.coms.com/package/package/package/vue-eventer 从VUE 2.X到VUE 3.0(只是初始化)时,代码会更改。-
&gt;
Using https://www.npmjs.com/package/vue-eventer means minimal code changes when migrating from Vue 2.x to Vue 3.0 (just initialization)...
->
有了Vue组成和定义,您甚至可以使其更容易:
我只是和一个孩子一起展示了它,但是您可以通过 emit 功能传递给其他孩子。
With Vue composition and defineEmit you can even make it easier :
I just showed it with one children, but you could pass though the emit function down to other children.
基于 mitt 库的简单解决方案。
eventbus.js
在项目中使用:
component-emiter.js
component-receiver.js
Simple solution based on mitt library.
EventBus.js
Usage in project:
Component-emiter.js
Component-receiver.js