Vue Js Custom Events

Posted on  by admin

You’re browsing the documentation for v2.x and earlier.

For v3.x, click here. We can use the v-on directive to listen to DOM events and run some JavaScript when they’re triggered.

The button above has been clicked {{ counter }} times.

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.

Instead of binding directly to a method name, we can also use methods in an inline JavaScript statement:.

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:.

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.

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.

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.

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.

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'.

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:.

.delete (captures both “Delete” and “Backspace” keys). 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:. You can use the following modifiers to trigger mouse or keyboard event listeners only when the corresponding modifier key is pressed:.

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”. 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.

The .exact modifier allows control of the exact combination of system modifiers needed to trigger an event.