Problem
When I add a new native event listener (el.addEventListener('scroll', () => blabla)
) on component mounting, should I remove it on component destroy? Or it will get removed automatically?
All the examples I find online are referred to window.addEventListener, which, as expected, do not get deleted at any time during the lifetime of the app and therefore require a manual listener cleanings.
My Code
I created a custom Vue directive to detect component scrolling event, and attached a listener on the insert hook, and wonder if I should also remove the listener on unbind hook.
My directive code:
bind(el, binding) {
el._scrollHandler = evt => binding.value(evt, el);
el.addEventListener('scroll', el._scrollHandler);
},
unbind(el) {
el.removeEventListener('scroll', el._scrollHandler);
}