Vue3 Fundamentals · Events & Forms

Responding to events

Handling events

Responding to events

A button on the page does nothing until something listens for a click. In Vue you attach that listener in the template with the v-on directive, written as @.

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

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

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

The @click part names the event; the value is the code to run when it fires. Here the handler is a short expression, count++. Vue also accepts a method name, @click="increment", and calls it with the event as its argument.

When the handler runs and changes reactive state, Vue updates the template for you. The button above shows 0 until you click it, then shows 1, because count changed and the {{ count }} interpolation re-rendered.

You can listen for any DOM event this way — @input, @submit, @keydown — and Vue wires the listener up when it mounts the component and removes it when the component unmounts, so the listener never leaks.

Your job is to make a button respond to a click in App.vue.

Challenge

A button is on the page but clicking it changes nothing. Export an App component from App.vue that renders a <button> showing a count. Start the count at 0 and increment it by one every time the button is clicked.

The Test reads the button text, clicks it, and expects the text to move from 0 to 1. The starter renders the count but never listens for the click, so the number never changes and the Test fails.

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

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

<template>
  <button>{{ 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.