I have vue2 components that are added and removed with v-if
. In some cases I communicate between them with $emit
from the sender, and $on
in the receiver.
I've been using this.$root.$emit
to broadcast custom events and this.$root.$on
to handle the events. I've discovered using this.$root.$on
requires this.$root.$off
(from beforeDestroy
), otherwise the removed component may attempt to handle it, and bad things happen.
I have two questions:
- What's the difference between
this.$root.$emit
andthis.$emit
? - If I use
this.$on
, do I also have to pair it withthis.$off
? Or will the handler automatically go "off" when the component is removed?