Vue Events List

Posted on  by admin

You’re browsing the documentation for v2.x and earlier. For v3.x, click here. This page assumes you’ve already read the Components Basics.

Read that first if you are new to components. Unlike components and props, event names don’t provide any automatic case transformation. Instead, the name of an emitted event must exactly match the name used to listen to that event.

Listening to Events

For example, if emitting a camelCased event name:. Listening to the kebab-cased version will have no effect:. Unlike components and props, event names will never be used as variable or property names in JavaScript, so there’s no reason to use camelCase or PascalCase.

Mouse Button Modifiers

Additionally, v-on event listeners inside DOM templates will be automatically transformed to lowercase (due to HTML’s case-insensitivity), so v-on:myEvent would become v-on:myevent – making myEvent impossible to listen to.

System Modifier Keys

For these reasons, we recommend you always use kebab-case for event names. By default, v-model on a component uses value as the prop and input as the event, but some input types such as checkboxes and radio buttons may want to use the value attribute for a different purpose.

Using the model option can avoid a conflict in such cases:. Now when using v-model on this component:.

the value of lovingVue will be passed to the checked prop. The lovingVue property will then be updated when emits a change event with a new value.

Note that you still have to declare the checked prop in the component’s props option.

There may be times when you want to listen directly to a native event on the root element of a component.

.prevent

In these cases, you can use the .native modifier for v-on:. This can be useful sometimes, but it’s not a good idea when you’re trying to listen on a very specific element, like an .

The second argument for the setup method is the context variable which contains three properties: attrs, slots, and most importantly for us, emit.

For example, the component above might refactor so that the root element is actually a

Syntax

In that case, the .native listener in the parent would silently break. There would be no errors, but the onFocus handler wouldn’t be called when we expected it to.

Key Codes

To solve this problem, Vue provides a $listeners property containing an object of listeners being used on the component.

Using the $listeners property, you can forward all event listeners on the component to a specific child element with v-on="$listeners".

.exact Modifier

For elements like , that you also want to work with v-model, it’s often useful to create a new computed property for listeners, like inputListeners below:.

Now the component is a fully transparent wrapper, meaning it can be used exactly like a normal element: all the same attributes and listeners will work, without the .native modifier.

Output

In some cases, we may need “two-way binding” for a prop. Unfortunately, true two-way binding can create maintenance issues, because child components can mutate the parent without the source of that mutation being obvious in both the parent and the child.

Example

Event Modifiers

That’s why instead, we recommend emitting events in the pattern of update:myPropName. For example, in a hypothetical component with a title prop, we could communicate the intent of assigning a new value with:.

Then the parent can listen to that event and update a local data property, if it wants to. For convenience, we offer a shorthand for this pattern with the .sync modifier:. Note that v-bind with the .sync modifier does not work with expressions (e.g.

Inline Statement Handler

v-bind:title.sync=”doc.title + ‘!’” is invalid).

Instead, you must only provide the name of the property you want to bind, similar to v-model.

  • The .sync modifier can also be used with v-bind when using an object to set multiple props at once:. This passes each property in the doc object (e.g. title) as an individual prop, then adds v-on update listeners for each one. Using v-bind.sync with a literal object, such as in v-bind.sync=”{ title: doc.title }”, will not work, because there are too many edge cases to consider in parsing a complex expression like this.

Click Event

Caught a mistake or want to contribute to the documentation?Edit this on GitHub!Deployed onNetlify . We can use the v-on directive to listen to DOM events:.

We are binding a click event listener to a method named greet.

  • shift

  • alt

  • ctrl

  • Here’s how to define that method in our Vue instance:. Test it yourself:.

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

Similar to the restrictions on inline expressions, event handlers are restricted to one statement only. Sometimes we also need to access the original DOM event in an inline statement handler.

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

Binding Native Events to Components

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.js provides two event modifiers for v-on: .prevent and .stop. Recall that modifiers are directive postfixes denoted by a dot:.

In 1.0.16, two additional modifiers have been introduced:.