Skip to the workspace
Pebellslearn by building
Forms Handling  ›  Lesson

v-model Modifiers

Modifiers 11 minThe lazy, number and trim modifiers change when v-model updates and how it cleans the text before storing it.

vue / v-model Modifiers

Explore each scene at your own pace.

01 · The rule

The rule

v-model Modifiers

Shelf's form adds a book with 320 pages, and the "pages left" total becomes '32010'. Someone else saves a title as " Dune " with stray spaces, and the search cannot find it. Both bugs come from raw input text. Three small modifiers on v-model fix them at the source.

The rule

.number converts, .trim cleans, .lazy waits.

<input v-model.trim="title" placeholder="Title" />
<input v-model.number="pages" placeholder="Pages" />
<textarea v-model.lazy="notes"></textarea>
  • .number passes the typed text through parseFloat, so '320' becomes the number 320. If the text cannot be parsed, such as 'abc', the original string is kept, so still validate. An input with type="number" gets this conversion automatically.
  • .trim removes whitespace from both ends: " Dune " is stored as "Dune".
  • .lazy changes when the ref updates. Normally v-model syncs on every input event, every keystroke. With .lazy it syncs on the change event, which for a text box fires when the user leaves the field (or presses Enter in a single-line input). Good for fields that trigger expensive work, like validation that calls a server.

Modifiers combine: v-model.lazy.trim="title" waits until the user leaves, then stores the trimmed text.

Why the '32010' bug happened: in JavaScript, + with a string joins text. '320' + 10 is '32010', while 320 + 10 is 330. Converting at the input means every later calculation gets a real number.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.