Chapter 13: Vue v-model

Vue v-model directive.

Imagine you’re building a form — a login screen, a todo input, a settings page, a checkout — and every time the user types something, you have to manually update your data, and every time your data changes (from an API or button), you have to push it back to the input. That sounds exhausting, right?

v-model solves exactly this pain by giving you true two-way data binding out of the box — clean, automatic, and magical-feeling when you first see it.

In Vue 3 (which we’re using in 2026), v-model works on:

  • Native form elements: <input>, <textarea>, <select>
  • Custom components (very important for reusable form fields)

Today I’ll explain it like we’re pair-programming in VS Code — step by step, with lots of examples, modifiers, gotchas, and real patterns.

1. What v-model Really Does (Under the Hood)

For a normal <input type=”text”>, this:

HTML

…is just syntactic sugar for:

HTML
  • :value pushes data down to the input
  • @input listens and pulls changes up to your data

Vue picks the right prop/event pair automatically depending on the element:

Element Prop used Event used Notes
<input type=”text”>, <textarea> value input Default
<input type=”checkbox”>, <input type=”radio”> checked change Boolean or array
<select> value change String or array (multiple)
Custom component modelValue (default) update:modelValue Vue 3 convention

2. Basic Example – Text Input (Hello World Style)

vue

→ Type in the input → message updates instantly → Click button → input updates automatically That’s two-way binding!

3. Modifiers – Make v-model Even Smarter

Vue gives three built-in modifiers:

  • .lazy → Update only on change (blur) instead of every keystroke
HTML
  • .number → Automatically convert to number (great for age, quantity)
HTML
  • .trim → Remove leading/trailing whitespace
HTML

You can combine them:

HTML

4. Checkbox & Radio (Common & Tricky)

Single checkbox (boolean):

vue
JavaScript

Multiple checkboxes (array):

vue
JavaScript

Radio buttons:

vue

5. Select (Single & Multiple)

vue

6. v-model on Custom Components (Super Important in 2026)

This is where v-model becomes really powerful for reusable form fields.

Modern pattern with defineModel() (recommended since Vue 3.4+ — cleanest way):

vue

Parent usage:

vue

Before defineModel() (still works, more verbose):

JavaScript

7. Multiple v-model on One Component (Vue 3 Superpower)

You can have several:

vue

Child must emit update:start, update:end, etc.

Quick Summary Table – v-model in 2026

Feature Syntax / Pattern Best For / Notes
Basic text v-model=”name” Simple inputs
Modifiers .lazy .number .trim Performance, type safety, clean data
Checkbox (single) v-model=”isActive” Boolean toggles
Checkbox (multiple) v-model=”hobbies” + :value Array collection
Custom component default v-model=”value” Uses modelValue + update:modelValue
Custom with defineModel() const model = defineModel() Cleanest modern way (3.4+)
Multiple bindings v-model:foo=”a” v-model:bar=”b” Date pickers, ranges, complex widgets

Pro Tips from Real Projects

  • Use reactive({}) for multi-field forms → easier than many refs
  • For validation → combine with computed + HTML5 attributes or libraries (VeeValidate 4, Zod + custom composable)
  • Avoid v-model on non-form elements — it’s meant for inputs/components
  • In large forms → extract fields to components with v-model support

Practice: Build a quick contact form with:

  • Name (trim)
  • Email (type=email)
  • Message (textarea)
  • Checkbox “Subscribe to newsletter”
  • Submit → console.log form data

Any part confusing? Want a full reactive form + validation example? Or v-model with file input? (special case) Or deep dive into custom component v-model with arguments?

Just tell me — we’ll keep going step by step 🚀

Happy binding from Hyderabad! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *