artemsky / vue-snotify

Vuejs 2 Notification Center
https://artemsky.github.io/vue-snotify/
MIT License
794 stars 98 forks source link

Duplicate notifications on route change (nuxt) #80

Closed leeovery closed 5 years ago

leeovery commented 5 years ago

Hi,

Im seeing notifications combine when changing routes with nuxt / vue-router.

Specifically I trigger a notification, change routes (to another page and then back again), and finally trigger the notifications again.

The first time I trigger the notification I see one popup as expected. It disappears as expected too. But the 2nd time I trigger the notification (as above) I see 2.

Any ideas on how to fix this?

The issue compounds too, if I keep repeating the sequence above, the number of notifications increases by 1 each time!!

Thanks Lee

leeovery commented 5 years ago

Ignore me. Im being super dumb sorry!!

marianocalvo commented 4 years ago

@leeovery I guess I'm being super dumb too, because I'm having the exact same issue! I am sure it is a silly mistake, but I can't find it. Do you remember what it was? Thanks so much.

leeovery commented 4 years ago

@marianocalvo Im so sorry but I really cant remember. Im looking through my code now. If I manage to recall what this was Ill update the thread. Hope you fix it.

marianocalvo commented 4 years ago

@leeovery Hi Lee, don't worry, I found it after a few hours of google researching!

The problem was that when you change to another route with the Vue router, the initial component is destroyed, but not its listeners... so when you go back to the first route, the initial component is created again, and a new duplicated listener, so the same event is listened twice (or three times, or four... depending on how many times you go back to the same route). Unexpected behavior for me.

I fixed it using $off to disable the listener for those events, from the beforeDestroy() component function.

Thank you for your interest!