Form Input Binding
Form input binding links an input box to a value in the script. The link keeps the box and the value in step with each other.
Link one input
v-model links this box to the ref named name.
<input v-model="name">Show the value
The paragraph shows what the ref holds.
<p>Hello, {{ name }}</p>The ref behind it
The ref starts as an empty string, so the box starts empty.
const name = ref('')Example
<script setup>
import { ref } from 'vue'
const name = ref('')
</script>
<template>
<div>
<label>Name <input v-model="name"></label>
<p>Hello, {{ name }}</p>
</div>
</template>Key points
- An input box is a place on the page where a person types text.
- v-model is a directive that links an input box to a ref.
- Typing in the box changes the ref value at once.
- Changing the ref value changes the text in the box.
In this example, name is a ref that starts as an empty string. The input uses v-model to link to name. The paragraph shows Hello, followed by the name. The page starts with an empty box and an empty greeting. Typing Ada in the box changes the ref to Ada. The paragraph then reads Hello, Ada.
Note
The box starts empty when the ref starts as an empty string.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does v-model do on an input box?