🎓 A1Learner
🔍
0 of 50 lessons completed

Event Modifiers

An event modifier is a small suffix that changes how an event is handled. It sits after the event name, following a dot.

Preventing a link

The click runs add without following the link.

<a href="https://example.com" @click.prevent="add">Add</a>

Stopping the spread

The click runs add without reaching the parent.

<button @click.stop="add">Add</button>

Running once

The click runs add only the first time.

<button @click.once="add">Add</button>

Example

<script setup>
import { ref } from 'vue'
const count = ref(0)
function add() {
  count.value++
}
</script>
<template>
  <div @click="add">
    <p>Count: {{ count }}</p>
    <button @click.stop="add">Add</button>
  </div>
</template>

Key points

  • .prevent stops the browser's default action, like following a link.
  • An event normally travels up to parent elements too.
  • .stop stops the event from reaching a parent element.
  • .once runs the handler only on the first event.
  • .self runs the handler only when the event starts on that element.
  • Modifiers can sit together on one event, like @click.stop.once.

In this example, count starts at zero. The div listens for clicks and runs add. The button also runs add, but .stop keeps its click from reaching the div. One button click adds one, not two. The paragraph shows the current count.

Note

Modifiers change the handling, not the markup the page shows.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does @click.prevent do on a link?