I am trying to call method on pressing enter key but it\'s not working. Code is as below.
Buttons don't have keyup event on them. Even when you have focus on the button, and hit enter, it will be considered a click event, instead of keyup.enter
.
Try binding the event to an input and it'd work.
Alternatively, you could use jQuery (or Plain JS) to bind for keydown event on the body
element, and trigger the Vue method by calling app.callEvent()
.
var app = new Vue({
el: "#app",
methods: {
callEvent() {
console.log("Event called");
}
},
mounted() {
var self = this;
window.addEventListener('keyup', function(event) {
if (event.keyCode === 13) {
self.callEvent();
}
});
}
});
Updated to use mounted
instead of relying on jQuery - as per Erick Petrucelli's answer as it allows referring to the Vue component without the global variable.