Vue v-model
Forms need data to flow in both directions: when your data changes, the input should show the new value; when the user types, your data should update. You could wire that up yourself with :value and @input, but Vue offers a shortcut: the v-model directive creates a two-way binding.
<input v-model="name">What v-model does under the hood
For a text input, v-model is short for a value binding plus an input listener:
<!-- these two lines behave the same -->
<input v-model="name">
<input :value="name" @input="name = $event.target.value">Try both in the playground — they stay in sync because they share the same data.
<div id="app">
<p><label>With v-model: <input v-model="name"></label></p>
<p><label>By hand: <input :value="name" @input="name = $event.target.value"></label></p>
<p>Hello, <strong>{{ name || 'stranger' }}</strong>!</p>
<button @click="name = 'Ana'">Set name to Ana</button>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({ data: () => ({ name: 'Kim' }) }).mount('#app');
</script>Note:
v-modelignores thevalue,checkedorselectedattributes written in the HTML. Your data is the single source of truth — set initial values indata()(orref()), not in the markup.
Live preview
A classic use of two-way binding is a live preview: whatever the user types appears immediately elsewhere on the page.
<style>
.card { margin-top: 10px; padding: 12px; border-radius: 8px; color: white; font-family: sans-serif; }
textarea { width: 260px; }
</style>
<div id="app">
<p><input v-model="title" placeholder="Title"></p>
<p><textarea v-model="body" rows="3" placeholder="Message"></textarea></p>
<p><input type="color" v-model="color"> {{ color }}</p>
<div class="card" :style="{ background: color }">
<h3>{{ title }}</h3>
<p style="white-space: pre-line">{{ body }}</p>
<small>{{ body.length }} / 120 characters</small>
</div>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({
title: 'Happy birthday!',
body: 'Have a wonderful day.\nSee you soon!',
color: '#42b883'
})
}).mount('#app');
</script>Note that <textarea> also uses v-model; writing {{ body }} between the textarea tags would not work.
v-model modifiers
| Modifier | Effect | Example |
|---|---|---|
.lazy |
Sync on change (when the field loses focus) instead of every keystroke |
v-model.lazy="city" |
.number |
Convert the value to a number with parseFloat |
v-model.number="age" |
.trim |
Remove whitespace from both ends | v-model.trim="username" |
<div id="app">
<p><label>Quantity (.number): <input v-model.number="qty" type="number"></label></p>
<p><label>Price (no modifier): <input v-model="price"></label></p>
<p>qty is a <code>{{ typeof qty }}</code>, price is a <code>{{ typeof price }}</code></p>
<p>qty * 2 = {{ qty * 2 }} | price + 1 = {{ price + 1 }}</p>
<p><label>Username (.trim): <input v-model.trim="user"></label> → "{{ user }}"</p>
<p><label>City (.lazy): <input v-model.lazy="city"></label> → {{ city }}</p>
</div>
<script type="module">
import { createApp } from 'vue';
createApp({
data: () => ({ qty: 2, price: '10', user: ' vue fan ', city: 'Hanoi' })
}).mount('#app');
</script>Type a new price: because it's a string, price + 1 concatenates ("101") instead of adding. The .number modifier avoids this bug.
Tip:
<input type="number">still gives you a string in JavaScript. Usev-model.numberwhenever you plan to calculate with the value.
v-model with the Composition API
With setup(), bind v-model to a ref. In the template you write the ref's name — Vue reads and writes .value for you.
<div id="app">
<input v-model="search" placeholder="Search fruit">
<ul>
<li v-for="f in fruits.filter(f => f.includes(search.toLowerCase()))" :key="f">{{ f }}</li>
</ul>
</div>
<script type="module">
import { createApp, ref } from 'vue';
createApp({
setup() {
const search = ref('');
const fruits = ['apple', 'banana', 'cherry', 'grape', 'mango', 'pineapple'];
return { search, fruits };
}
}).mount('#app');
</script>v-model works with every kind of form control — checkboxes, radio buttons, selects — and even with your own components. The next lesson covers the different input types.
Test yourself
Which pair of bindings does v-model replace on a text input?
:valueand@input:checkedand@click:modeland@changev-bindandv-if
The user types 42 into <input v-model="age">. What is typeof age?
numberstringbigintundefined
Which modifier updates the data only when the input loses focus?
.trim.once.lazy.blur