DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • When Feature Flags Become Workflows: Configuring Multi-Tenant State Machines
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • How Much Signal Differential Privacy Takes From Your Dashboards, in Events
  • Consumer Lag Is the Wrong Finish Line

Trending

  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views
  • How to Format Articles for DZone
  • Introducing senro: A Pipeline Engine You Program in Go
  • How to Migrate From C/C++ to Rust: A Practical Guide for Embedded IoT Systems

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

A Guide to Events in Vue

Let's look at how events work in Vue.

By 
Johnny Simpson
·
May. 13, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
5.3K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

As with any framework, Vue lets us add reactivity to our applications and websites through events. The great thing about Vue events is they mimic vanilla Javascript, so all the events you're used to using in Javascript can also be used in Vue.

Vue Basics

If you are brand new to Vue, I would recommend reading my guide on making your first Vue application or creating components in Vue first.

Events in Vue

The most basic event frequently used in Vue, as well as in most Javascript, is click. The component below is a simple counter which increases by 1 every time the button is clicked. To do this, we use an inline @click event:

Vue.js Component
 
<script>
export default {
    data() {
        return {
            counter: 0
        }
    }
}
</script>
<template>
    <button @click="++counter">
        {{ counter }}
    </button>
</template>

Since we can write inline Javascript straight into our events, we can simply write ++counter to increase our counter data. As such, the above will increase counter any time we click the button, and display that in our button element.

As mentioned before, we aren't just limited to @click. All other Javascript events work too, in the same format. That means you can use:

  • @keydown
  • @mousedown
  • @pointerdown
  • @pointerup
  • @scrolletc.

We aren't just limited to running Javascript inline in our events. We can trigger a method or function if one is defined in our Vue Javascript. Here is the same code using a method instead:

Vue.js Component
 
<script>
export default {
    data() {
        return {
            counter: 0
        }
    },
    methods: {
        incrCounter: function() {
            ++this.counter
        }
    }
}
</script>
<template>
    <button @click="incrCounter">
        {{ counter }}
    </button>

V-on Vs @ in Vue

You may have seen events written as v-on:click vs @click. Both of these mean the same thing and are interchangeable, so use whichever one you are comfortable with!

Mouse Specific EVents

We can further modify any mouse events by using the left, middle, and right modifiers. If we are firing a mouse-related event, like click, or mousedown, then mousedown.right will only track right mouse clicks, or mousedown.middle will only track middle mouse clicks.

Vue.js Component
 
<!-- left mouse clicks -->
<button @mousedown.left="incrCounter">
    {{ counter }}
</button>
<!-- right mouse clicks -->
<button @mousedown.right="incrCounter">
    {{ counter }}
</button>
<!-- middle mouse clicks -->
<button @mousedown.middle="incrCounter">
    {{ counter }}
</button>

Using Event Data in Vue Events

Sometimes, we want to access the event or e object in our events. In situations where we simply want to access e itself with no other arguments, we don't have to mention e - it is automatically passed directly to the function. For example, the code below will still console log e.clientX and e.clientY whenever the user clicks the button:

Vue.js Component
 
<script>
export default {
    data() {
        return {
            counter: 0
    }
  },
  methods: {
    incrCounter: function(e) {
      ++this.counter
      console.log(e.clientX, e.clientY)
    }
  }
}
</script>

<template>
    <button @click="incrCounter">
    {{ counter }}
  </button>
</template>

Things become a little trickier when you have more than 2 arguments. In these situations, there are two ways to access event data. Either encapsulate the function or use the predefined $event variable.

For example, let's say we want to increase the counter by a custom amount, and continue to console log e.clientX and e.clientY. This is achievable by using $event to pass the event data to our function:

Vue.js Component
 
<script>
export default {
    data() {
        return {
            counter: 0
        }
    },
    methods: {
        incrCounter: function(amount, e) { 
            ++this.counter
            console.log(e.clientX, e.clientY)
        }
    }
}
</script>

<template>
    <button @click="incrCounter(5, $event)">
        {{ counter }}
    </button>
</template>

Alternatively, we could also pass the e object directly to the function as follows:

Vue.js Component
 
<button @click="(e) => incrCounter(5, e)">
    {{ counter }}
</button>

Custom Key Events in Vue Events

Vue tries to simplify events as much as possible. If you've ever made key events in the past, you'll know that frequently we only want to access a specific key. Therefore, with key events, we can tie common keys directly to the event. For example, in this input, we will fire an event any time the user presses a keyup event:

HTML
 
<input @keyup="someFunction" />

But if we want to fire the @keyup only when the user presses enter, we can do that with the following event:

HTML
 
<input @keyup.enter="someFunction" />

We can use any defined keyboard key value, converted to a kebab case. For example, PageDown is a keyboard key defined value, but in Vue, we write page-down:

HTML
 
<input @keyup.page-down="someFunction" />

Finally, Vue has defined some commonly used keys which are not defined values. These are enter, tab, delete, esc, space, up, down, left, right, as well as the keyboard modifiers ctrl, alt, shift and meta.

Keyboard Modifiers and Keys

We just mentioned the keyboard modifiers ctrl, alt, shift and meta, and these can actually be combined with our key values from before, to add another layer of functionality. For example, the below will fire the keydown event, and therefore someFunction, whenever shift and enter are both pressed within the input:

HTML
 
<input @keydown.shift.enter="someFunction" />

Exact Modifier

Finally, we can make sure only one key is being pressed by using exact. The below, for example, will only fire if enter is pressed alone. If enter is pressed with a combination of other keys, the event will not fire:

HTML
 
<input @keydown.enter.exact="someFunction" />

Final Thoughts

Event control in Vue is an essential element in building any complete Vue application. I hope you've enjoyed this guide.

Event

Published at DZone with permission of Johnny Simpson. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • When Feature Flags Become Workflows: Configuring Multi-Tenant State Machines
  • Running a Self-Hosted WAF in Production: Practical Lessons From SafeLine
  • How Much Signal Differential Privacy Takes From Your Dashboards, in Events
  • Consumer Lag Is the Wrong Finish Line

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook