← Back to Vue.js Course | Chapter 2: Template Syntax | Lesson 6 of 7

v-on (@)

v-on listens to DOM events such as click and input, and @ is its short form.

In this page:

  1. v-on (@)
Syntax
markup
<button @click="handler">text</button>
<button v-on:click="count++">text</button>

v-on (@)

Write @click="handler" or @click="count++" to react to events. Handlers receive the native event, and you can pass arguments with $event. Modifiers like .prevent, .stop, .once and key modifiers such as .enter keep handlers clean.

Note: @submit.prevent stops the browser from reloading the page.

Example: v-on (@)

markup
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
</head>
<body>
<div id="app">
  <button @click="hits++">Hits: {{ hits }}</button>
  <button @click="greet('Ada', $event)">Greet</button>
  <button @click.once="onceCount++">Once: {{ onceCount }}</button>
  <p>{{ log }}</p>
</div>
<script>
  Vue.createApp({
    data() { return { hits: 0, onceCount: 0, log: "" }; },
    methods: { greet(name, e) { this.log = "Hello " + name + " from " + e.type; } },
  }).mount("#app");
  const [b1, b2, b3] = document.querySelectorAll("button");
  b1.click(); b1.click(); b2.click(); b3.click(); b3.click();
  Vue.nextTick(() => console.log(document.querySelector("#app").textContent.replace(/\s+/g, " ").trim()));
</script>
</body>
</html>

<!-- Output:
Rendered: Hits: 2GreetOnce: 1Hello Ada from click
console: Hits: 2GreetOnce: 1Hello Ada from click
-->
Live Example
Related Topics
Common Mistakes
  1. Calling the method with parentheses by mistake in attributes
  2. Forgetting .prevent on form submits
  3. Writing onclick instead of @click
Chapter Summary
  • @event listens to DOM events
  • $event is the native event
  • Modifiers: prevent, stop, once
  • Key modifiers like .enter
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.