Vue3 Fundamentals ยท Events & Forms

Event modifiers

Event modifiers

Some events arrive with behaviour you usually want to stop: a link navigates, a form reloads the page, a click bubbles up to a parent. You could call event.preventDefault() inside the handler, but Vue lets you say it in the template instead, with a modifier.

A modifier is a dot-suffixed word on the event:

<a @click.prevent="go" href="/next">Next</a>
<button @click.stop="pick">Pick</button>
<button @click.once="start">Start</button>
  • .prevent calls event.preventDefault() for you.
  • .stop calls event.stopPropagation(), so the event does not bubble.
  • .once runs the handler at most one time.

Modifiers keep the intent next to the event, where you read it, instead of burying it in the handler body. You can chain them, and they run in the order you write them.

The .once modifier is the clearest to see: the handler fires on the first click and never again, so a counter it updates stops at one no matter how many times you click.

Your job is to make a button count only the first click in App.vue.

Challenge

A button is counting every click, but it should count only the first one. Export an App component from App.vue that renders a <button> showing a count. Start at 0 and increment it by one, but only on the first click.

The Test clicks the button twice and expects the text to be 1, not 2. The starter listens on every click, so the count reaches 2 and the Test fails.

App.vue
<script setup>
import { ref } from 'vue';

const count = ref(0);
</script>

<template>
  <button @click="count++">{{ count }}</button>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge โ€” you only see the result, never the test source.