[Vuejs]-I'm listening for a "Ctrl+Q" keyup event. I have an alert and a function. The alert runs but the function does not

0👍

Possible Solutions

1.By binding this reference to function

created() {
    document.addEventListener('keyup', (function(event) {
      if (event.code == 'KeyQ' && (event.ctrlKey || event.metaKey)) {
        alert("asdfasdfasfd")
        this.clear()
      }
    }).bind(this))
  },

2.use of arrow function

created() {
    document.addEventListener('keyup', (event) => {
      if (event.code == 'KeyQ' && (event.ctrlKey || event.metaKey)) {
        alert("asdfasdfasfd")
        this.clear()
      }
    })
  },

Leave a comment