Vue Tutorial
The v-on directive in Vue instructs the browser, much like event handling in standard JavaScript:
To show how v-on may be used with various events and different code to run when these events occur, let’s look at a few samples.
Let’s look at a few examples to demonstrate how v-on may be used with different events and different code to run when these events occur.
We can take action in response to predefined events with the v-on directive.
To take action when the element is clicked, use v-on:click.
The <button> element uses the v-on directive to listen for the ‘click’ event. The yellow <div> behind the lightbulb is either visible or hidden depending on whether the ‘click’ event causes the ‘lightOn’ data property to be toggled between ‘true’ and ‘false’.
To take action when an element receives input—such as a keystroke inside a text field—use v-on:input.
Determine how many keystrokes were made in an input text field:
{{ 'Input events occured: ' + inpCount }}
To take action when the mouse pointer passes over an element, use v-on:mousemove.
Whenever the mouse pointer passes over a <div> element, its background color can be changed:
Move the mouse pointer over the box below
The v-on directive can also be utilized within a v-for loop.
Every iteration within the v-on value has access to the array’s items.
Show a list based on the food array, and then add a click event to each item such that it can modify the image’s source by using a value from the array item.
-
{{ food.name }}
‘@‘ is the shorthand for ‘v-on‘.
Here, ‘@‘ is used in place of ‘v-on‘:
A bit later in this course, we will begin using the @ syntax.
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.