Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are listeners removed when directive is destroyed?

Tags:

vue.js

Here's a simple options object for a directive. When the directive is destroyed what happens to the event listener? Is it stuck in memory or is it removed?

export default {
  inserted (el) {
    el.addEventListener('click', function () {
      console.log('Click!')
    })
  }
}
like image 834
manidos Avatar asked Oct 27 '25 23:10

manidos


1 Answers

No, that's your responsibility, in the unbind hook

function listener () {
  console.log('Click!')
}

export default {
  inserted (el) {
    el.addEventListener('click', listener)
  },
  unbind(el) {
    el.removeEventListener('click', listener)
  }
}
like image 112
Linus Borg Avatar answered Oct 29 '25 18:10

Linus Borg