E
E
Emil Rakhmatullin2021-10-07 15:07:45
Vue.js
Emil Rakhmatullin, 2021-10-07 15:07:45

Is the Vue.js global event bus OK?

Is it normal practice to use the event bus globally in large projects?
app.js:

import Vue from 'vue'

Vue.prototype.$bus = new Vue()

new Vue({
  el: '#app',
})

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Emil Rakhmatullin, 2021-10-07
@Emchik

Instead of an event bus, it's better to use Vuex

A
Andrew, 2021-10-07
@AndrewRusinas

No, this is a miserable anti-pattern that you need to run away from.

0
0xD34F, 2021-10-07
@0xD34F Vue.js

This thing has been deprecated for several years now .

A
Aetae, 2021-10-07
@Aetae

If you ignore the witch hunt, then you should use the event bus when events are needed . If you need a state and its change - the event bus is really an antipattern, but in this case.
However, option c, bus = new Vue()although simple, is fraught with memory leaks. It is better to make the bus through a global mixin, which, on destroy, will automatically clear all listeners of a particular component.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question