Vue3 Fundamentals · Events & Forms

Two-way binding on a text input

Two-way binding with v-model

Two-way binding on a text input

A text input holds a value the user types, and your component often needs that value too. Wiring it by hand means reading the element on every input event and writing the value back when state changes — two directions to keep in sync.

Vue gives you one directive for both directions: v-model.

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

const name = ref('');
</script>

<template>
  <input v-model="name" />
  <p>{{ name }}</p>
</template>

v-model="name" binds the input to the name ref. When the user types, Vue writes the new value into name; when name changes in code, Vue writes it back into the input. The paragraph shows the value, so you can watch it update as you type.

Under the hood v-model is shorthand: it sets the input's value from the ref and listens for the input event to write back. You get that pairing without writing either half.

The ref starts as an empty string, so the input and the paragraph both begin empty. Your job is to bind them in App.vue.

Challenge

The input on the page is not connected to the text below it. Export an App component from App.vue that renders a text <input> and a <p>. Bind the input to a ref with v-model, and render that ref inside the paragraph.

The Test types Vue into the input and expects the paragraph to read Vue. The starter renders an input with no binding, so the paragraph stays empty and the Test fails.

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

const name = ref('');
</script>

<template>
  <input />
  <p>{{ name }}</p>
</template>

No test run yet

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