avatar
DOM element click event in Vue JS Vue
<h2 id="header">Home Page</h2>

> Using addEventListener and removeEventListener

mounted() {
  this.$refs['header'].addEventListener('click', (e) => {
    // Do stuff
  }
},
beforeDestroy() {
  this.$refs['header'].removeEventListener('click'), (e) => {
    // Do stuff
  }
}

> Using nextTick if the element is still undefined with the code but inserted into the DOM

mounted() {
  this.$nextTick(function() {
    this.$refs['header'].addEventListener('click', (e) => {
      // Do stuff
    }
  });
},
24
You need to login to do this manipulation!