
Since it’s difficult to remember all of the key codes, Vue provides a set of pre-defined keys.
#USE EVENT KEY OUTSIDE KEYUP FUNCTION JAVASCRIPT CODE#
In the above example, when the keyup event is fired with the key code of 13 (the enter key), the addToCount method gets called. window.addEventListener('keydown', keyDownHandler) The keydown event triggers continuously while the key is pressed. Similar to event modifiers, we can add key modifiers that allow us to listen to a particular key when handling key-related events such as keyup. Attach a listener to the keydown event instead of keypress, since the latter is now deprecated.
self - Only trigger if the target of the event is itself. capture - Capture mode is used for event handling. stop - Prevents event bubbling up the DOM tree. The following modifiers are available in Vue. If we didn’t add the modifier, the page would try to re-direct to the path defined in the href attribute. The above code sample would remove the default behavior of the a tag and just call the addToCount method. Instead of having to write these out in the methods, we can use the modifiers provided with the vue-on directive. Vue has made it easier for us to implement these by using modifiers.įor example, event.preventDefault() is often called when handling events to prevent the browsers default behavior. There are frequently used calls that are made when handling events. Rather than having to declare events using the v-on: syntax, we can use the symbol instead: Add Event Modifiers The addToCount method will take the input from addValue and add that to the count. Let’s start off by initializing a counter to zero in the data model: data ( ) We can use the v-on directive to handle these events.Īs a simple example, we can implement a counter that increments every time a user clicks a button. User interactions with the view can trigger events on the DOM such as click and keyup. Vue provides us with the v-on directive to handle these events. Handling events helps add interactivity to web apps by responding to the user’s input. Vue.js allows us to handle events triggered by the user.