cklmercer / vue-events

Simple event handling for Vue.js
MIT License
234 stars 27 forks source link

$off not working Vue.js 2 #11

Closed sn0rcha closed 7 years ago

sn0rcha commented 7 years ago

Hey There,

Thanks for the plugin, I'm having an issue unsubscribing from events tho.

In one component route I have in the mounted function:

this.$events.$on('integrations-xpm-connected', (data) => {
       console.log('test');
      });

Then in the beforeDestroy function:

this.$events.$off('integrations-xpm-connected');

However, if I navigate to a new route, then come back, the event gets fired twice when called.

if I call this.$events.$off() with no particular event specified, it unsubscribes.

Any ideas?

cklmercer commented 7 years ago

sorry about the delay, for some reason I never got a notification for this issue.. that or I overlooked it terribly.

I'm not sure off the top of my head, but I'll try and get around to writing some tests for this soon. In the mean time, if you figure it out then I'd really appreciate a PR. :)

christophemarois commented 7 years ago

@sn0rcha @cklmercer I'm having this issue too, but I don't think it has to do with $off, but rather the lifetime of Vue components. If you have two of the same component alive at the same time, and you're listening to an event on both of them, your event handler will effectively be called twice.

EDIT: for instance-based event connection, this technique might be more useful than the global event bus of vue-events

sandeepk01 commented 7 years ago

Tried to resolve this issue using https://www.npmjs.com/package/vue-event-handler Appreciate any success.

vikashsd commented 7 years ago

Thanks for the idea, worked well for me.