Like the EventBus
opera?
Communication between components
Any and all communication between components is available in the Vue
and you can find them giving a console.log(this)
inside the component, that’s where Props
and EventBus
operate, they simply execute methods in the instance you could do yourself, but in a much more organized way and risk saying that often even unnecessary...
Eventbus
Before understanding how it works it is necessary to know if you really need to use, when it is necessary to execute methods on other components from an action in the central component, we can use the EventBus
, it automatically operates on any component that is associated with the component, be it $parent
or $children
, it’s like a global spread, who wants to listen listen listen who doesn’t want to stay quiet kk...
In practice
Let’s think and a structure like this
src
- App.
- Propag.Vue
- Event-bus.js
- main.js
Main rendered the App.Vue
Event.bus.js
import Vue from "vue";
export const EventBus = new Vue();
App.
<template>
<div>
<div class="pleeease-click-me" @click="emitGlobalClickEvent()">Click para emitir</div>
<Propag ref="prop"></Propag>
</div>
</template>
<script>
import { EventBus } from "./event-bus.js"; // EventBus
import Propag from "./Propag.vue";
export default {
components: { Propag },
data() {
return {
clickCount: 0
};
},
methods: {
emitGlobalClickEvent() {
this.clickCount++;
this.$children[0].somaClickComp(this.clickCount); // Simples
EventBus.$emit("somaClick", this.clickCount); // EventBus
}
}
};
</script>
<style>
div.pleeease-click-me {
width: 130px;
height: 20px;
background: black;
color: white;
display: flex;
align-items: center;
justify-content: center;
padding: 5px;
}
</style>
Propag.Vue
<template>
<div class="other">
Atualizado pelo EventBus: {{clicks}} <br>
Atualizado pelo componente : {{compClick}}
</div>
</template>
<script>
import { EventBus } from "./event-bus.js"; // EventBus
export default {
created() { // EventBus
EventBus.$on("somaClick", this.somaClick);
},
data() {
return {
clicks: 0,
compClick: 0
};
},
methods: {
somaClick(item) { // EventBus
this.clicks = item;
},
somaClickComp(item) { // Simples
this.compClick = item;
}
}
};
</script>
<style>
.other {
margin-top: 30px;
width: 250px;
height: 40px;
background: black;
color: white;
display: flex;
align-items: center;
justify-content: center;
padding: 5px;
}
</style>
In the parts where I use the EventBus
and the simple method is marked with a comment identifying, note that the EventBus
is more verbose and complicated, in the long run will help you? in my opinion helps only in the centralization of events, ie, you would not need to worry in pointing out the component that will receive the event, just care it, I particularly do not use, because it helps me a lot the practicality and the organization of simple communication.
Regarding the second way of doing, I have no knowledge to speak...
Code running: https://codesandbox.io/s/mm58vw8px8
I will test these different implementations later. Even with mixin I access it with this. $bus?
– guastallaigor
@guastallaigor as its name (Mixed in) suggests, will perform a merge of the properties (whether the Hooks, methods, events, date, etc.) with the properties of the instance of Vue. so let’s say we have a Mixin with hook Mounted and this mixin is added to a certain component, so whenever this component is mounted, the Mixin hook Mounted will run, as well as the component’s own hook Mounted. In the above example Mixin has been registered globally, but it can be recorded individually, just like with the components.
– Tobias Mesquita