Listening for Events
You can use the
v-on directive to bind event listeners to DOM events. It can be bound to either an event handler function (without the invocation parentheses) or an inline expression. If a handler function is provided, it will get the original DOM event as the argument. The event also comes with an extra property:
targetVM, pointing to the particular ViewModel the event was triggered on:
Invoke Handler with Expression
targetVM could be useful when
v-on is used with
v-repeat, since the latter creates a lot of child ViewModels. However, it is often more convenient and explicit to use an invocation expression passing in the current alias, which equals the current data object being iterated on:
When you want to access the original DOM event in an expression handler, you can pass it in as
<button v-on="click: submit('hello!', $event)">Submit</button>
/* ... */
When listening for keyboard events, we often need to check for common key codes. Vue.js provides a special
key filter that can only be used with
v-on directives. It takes a single argument that denotes the key code to check for:
<!-- only call vm.submit() when the keyCode is 13 -->
It also has a few presets for commonly used keys:
<!-- same as above -->
Check the API reference for a full list of key filter presets.
Why Listeners in HTML?
You might be concerned that this whole event listening approach violates the good old rules about “separation of concern”. Rest assured - since all Vue.js handler functions and expressions are strictly bound to the ViewModel that’s handling the current View, it won’t cause any maintainance difficulty. In fact, there are several benefits in using
- It makes it easier to locate the handler function implementations within your JS code by simply skimming the HTML template.
- Since you don’t have to manually attach event listeners in JS, your ViewModel code can be pure logic and DOM-free. This makes it easier to test.
- When a ViewModel is destroyed, all event listeners are automatically removed. You don’t need to worry about cleaning it up yourself.
Next up: Handling Forms.