The Vue.js method is called reusing $ emit and $ on when it needs to be called only once.

I use a bus to allow components to interact with other components using the method described in this link: https://forum.vuejs.org/t/create-event-bus-in-webpack-template/4546/2 .

I have a method that is called in a created hook that uses a bus to dispatch an event.

created () {
  this.getReviewDeck()
},
myMethod () {
    bus.$emit('increment')
}

In another component (which is contained in the above component) I attach an event listener to the created hook, as shown below:

created () {
  bus.$on('increment', this.incrementCount)
},
incrementCount () {
  console.log('count incremented')
}

, , " " . , , " " , , ..

, , , , , .

+10
3

.

 beforeDestroy () {
    EventBus.$off('increment', this.incrementCount)
 },
+30

$.once

created () {
  bus.$once('increment', this.incrementCount)
},
+7

, . , , : , - . $ . , , $ on, , .

;

this.$eventBus.$emit("SHOW_HOVERLINE", d);

;

this.$eventBus.$on("SHOW_HOVERLINE", this.someFunction);
+1

Source: https://habr.com/ru/post/1667907/


All Articles