I have recently delved into the world of vuejs
and discovered that in order to send data from a child component back to its parent, we can use
this.$root.$emit('name-of-event', myobject);
The parent component can then receive this data by using this.$root.$on('name-of-event');
However, in another vuejs project I'm working on for comparison purposes, I noticed that the component listening to the event is not always the direct parent. The component triggering the event doesn't necessarily need to be rendered within the same component listening to it.
This made me wonder: does the listener for emitted events always have to be the direct parent? Is it possible for other components to listen to these events as well?
myAcomponent.vue :
updateDate(value) {
//body of updateDate method
this.$root.$emit('date-updated', this.project);
}
myBcomponent.vue :
<script>
created() {
this.$root.$on('date-updated', project => {
this.updateproject(project);
});
}
</script>
<template>
//no call in template for myAcomponent
</template>