Guide
Essentials
- Installation
- Introduction
- The Vue Instance
- Template Syntax
- Computed Properties and Watchers
- Class and Style Bindings
- Conditional Rendering
- List Rendering
- Event Handling
- Form Input Bindings
- Components Basics
Components In-Depth
- Component Registration
- Props
- Custom Events
- Slots
- Dynamic & Async Components
- Handling Edge Cases
Transitions & Animation
- Enter/Leave & List Transitions
- State Transitions
Reusability & Composition
- Mixins
- Custom Directives
- Render Functions & JSX
- Plugins
- Filters
Tooling
- Single File Components
- Testing
- TypeScript Support
- Production Deployment
Scaling Up
- Routing
- State Management
- Server-Side Rendering
- Security
Internals
- Reactivity in Depth
Migrating
- Migration from Vue 1.x
- Migration from Vue Router 0.7.x
- Migration from Vuex 0.6.x to 1.0
- Migration to Vue 2.7
Meta
- Comparison with Other Frameworks
- Join the Vue.js Community!
- Meet the Team
Event Handling
Listening to Events
We can use the v-on
directive to listen to DOM events and run some JavaScript when they’re triggered.
For example:
|
|
Result:
The button above has been clicked {{ counter }} times.
Method Event Handlers
The logic for many event handlers will be more complex though, so keeping your JavaScript in the value of the v-on
attribute isn’t feasible. That’s why v-on
can also accept the name of a method you’d like to call.
For example:
|
|
Result:
Methods in Inline Handlers
Instead of binding directly to a method name, we can also use methods in an inline JavaScript statement:
|
|
Result:
Sometimes we also need to access the original DOM event in an inline statement handler. You can pass it into a method using the special $event
variable:
|
|
Event Modifiers
It is a very common need to call event.preventDefault()
or event.stopPropagation()
inside event handlers. Although we can do this easily inside methods, it would be better if the methods can be purely about data logic rather than having to deal with DOM event details.
To address this problem, Vue provides event modifiers for v-on
. Recall that modifiers are directive postfixes denoted by a dot.
.stop
.prevent
.capture
.self
.once
.passive
|
Order matters when using modifiers because the relevant code is generated in the same order. Therefore using v-on:click.prevent.self
will prevent all clicks while v-on:click.self.prevent
will only prevent clicks on the element itself.
New in 2.1.4+
|
Unlike the other modifiers, which are exclusive to native DOM events, the .once
modifier can also be used on component events. If you haven’t read about components yet, don’t worry about this for now.
New in 2.3.0+
Vue also offers the .passive
modifier, corresponding to addEventListener
‘s passive
option.
|
The .passive
modifier is especially useful for improving performance on mobile devices.
Don’t use .passive
and .prevent
together, because .prevent
will be ignored and your browser will probably show you a warning. Remember, .passive
communicates to the browser that you don’t want to prevent the event’s default behavior.
Key Modifiers
When listening for keyboard events, we often need to check for specific keys. Vue allows adding key modifiers for v-on
when listening for key events:
|
You can directly use any valid key names exposed via KeyboardEvent.key
as modifiers by converting them to kebab-case.
|
In the above example, the handler will only be called if $event.key
is equal to 'PageDown'
.
Key Codes
The use of keyCode
events is deprecated and may not be supported in new browsers.
Using keyCode
attributes is also permitted:
|
Vue provides aliases for the most commonly used key codes when necessary for legacy browser support:
.enter
.tab
.delete
(captures both “Delete” and “Backspace” keys).esc
.space
.up
.down
.left
.right
A few keys (.esc
and all arrow keys) have inconsistent key
values in IE9, so these built-in aliases should be preferred if you need to support IE9.
You can also define custom key modifier aliases via the global config.keyCodes
object:
|
System Modifier Keys
New in 2.1.0+
You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressed:
.ctrl
.alt
.shift
.meta
Note: On Macintosh keyboards, meta is the command key (⌘). On Windows keyboards, meta is the Windows key (⊞). On Sun Microsystems keyboards, meta is marked as a solid diamond (◆). On certain keyboards, specifically MIT and Lisp machine keyboards and successors, such as the Knight keyboard, space-cadet keyboard, meta is labeled “META”. On Symbolics keyboards, meta is labeled “META” or “Meta”.
For example:
|
Note that modifier keys are different from regular keys and when used with keyup
events, they have to be pressed when the event is emitted. In other words, keyup.ctrl
will only trigger if you release a key while holding down ctrl
. It won’t trigger if you release the ctrl
key alone. If you do want such behaviour, use the keyCode
for ctrl
instead: keyup.17
.
.exact
Modifier
New in 2.5.0+
The .exact
modifier allows control of the exact combination of system modifiers needed to trigger an event.
|
Mouse Button Modifiers
New in 2.2.0+
.left
.right
.middle
These modifiers restrict the handler to events triggered by a specific mouse button.
Why Listeners in HTML?
You might be concerned that this whole event listening approach violates the good old rules about “separation of concerns”. Rest assured - since all Vue handler functions and expressions are strictly bound to the ViewModel that’s handling the current view, it won’t cause any maintenance difficulty. In fact, there are several benefits in using v-on
:
It’s easier to locate the handler function implementations within your JS code by 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.