Answer the question
In order to leave comments, you need to log in
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
No, this is a miserable anti-pattern that you need to run away from.
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 questionAsk a Question
731 491 924 answers to any question