Mojtaba Barari
Mojtaba Barari

Reputation: 1274

removing event listeners in Nuxt/Vue

I'm on Nuxtjs 2.13 and i wanna know "how should I remove event listeners (is there a need??)".

I'm not talkinkg about js addEventListener and removeEventListener . I'm more curious about this.$emit() , $nuxt.$emit() and $nuxt.$on() . is there a way to remove $nuxt.$on() or listener on component <mycomp @myevent="do()" /> in beforeDestroy() and is it necessary?

as my Nuxt project using so much RAM on my server, i kindda think there are some optimization needed.

Upvotes: 3

Views: 6461

Answers (1)

j-toscani
j-toscani

Reputation: 106

https://v3.vuejs.org/api/options-lifecycle-hooks.html#unmounted

When this hook (unmounted - OP) is called, all directives of the component instance have been unbound, all event listeners have been removed, and all child component instance have also been unmounted.

However, there is vm.$off which can

Remove custom event listener(s). https://v2.vuejs.org/v2/api/#vm-off

I saw it used here in a Nuxt context to remove $nuxt.$on listeners: https://medium.com/@aneesshameed/event-bus-in-nuxt-7728315e81b6

So, if need be, use $nuxt.$off to remove custom events in Nuxt.

Upvotes: 5

Related Questions