v-model modifiers help you control how and when Vue updates data from user input. They make input handling easier and cleaner.
v-model modifiers (lazy, number, trim) in Vue
<input v-model.lazy="dataProperty" /> <input v-model.number="dataProperty" /> <input v-model.trim="dataProperty" />
Modifiers are added after v-model with a dot, like .lazy.
You can combine modifiers, for example: v-model.lazy.number.
name only when the input loses focus (on change event).<input v-model.lazy="name" />age.<input v-model.number="age" />username.<input v-model.trim="username" /><input v-model.lazy.number.trim="score" />This Vue component shows three inputs using v-model with different modifiers. You can see how each modifier changes when and how the data updates.
<template>
<div>
<label for="lazyInput">Lazy Input (updates on blur):</label>
<input id="lazyInput" v-model.lazy="lazyText" />
<p>Value: {{ lazyText }}</p>
<label for="numberInput">Number Input (converts to number):</label>
<input id="numberInput" v-model.number="numberValue" />
<p>Type: {{ typeof numberValue }}, Value: {{ numberValue }}</p>
<label for="trimInput">Trim Input (removes spaces):</label>
<input id="trimInput" v-model.trim="trimText" />
<p>Value: '{{ trimText }}'</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const lazyText = ref('')
const numberValue = ref(0)
const trimText = ref('')
</script>.lazy waits for the input to lose focus before updating the data, reducing updates while typing.
.number tries to convert the input to a number; if conversion fails, it keeps the original value.
.trim removes spaces only at the start and end, not inside the text.
v-model modifiers help control input behavior easily.
.lazy updates on change, .number converts to number, .trim removes spaces.
Use them to make forms cleaner and user-friendly.
Practice
.lazy modifier do when used with v-model in Vue?Solution
Step 1: Understand the default
By default,v-modelbehaviorv-modelupdates the bound data on every input event (each keystroke).Step 2: Effect of
The.lazymodifier.lazymodifier changes this behavior to update only when the input loses focus or on the change event.Final Answer:
Updates the bound data only when the input loses focus or on change event -> Option DQuick Check:
.lazydelays update until change [OK]
- Thinking .lazy trims spaces
- Assuming .lazy converts to number
- Believing .lazy prevents updates
v-model with the .number modifier on an input element?Solution
Step 1: Recall correct modifier syntax
Vue modifiers are added afterv-modelwith a dot, likev-model.number.Step 2: Identify correct syntax
The correct syntax is<input v-model.number="age">. Placing the modifier inside quotes, using a function call, or adding it as a separate attribute is incorrect.Final Answer:
<input v-model.number="age"> -> Option AQuick Check:
Modifier syntax = dot after v-model [OK]
- Placing modifier inside quotes
- Using modifier as a property of variable
- Adding modifier as a separate attribute
<input v-model.trim="username">
<p>User: {{ username }}</p>If the user types
alice (with spaces before and after) and then the input loses focus, what will be displayed inside the paragraph?Solution
Step 1: Understand
The.trimmodifier effect.trimmodifier removes whitespace from the start and end of the input value before updating the bound data.Step 2: Apply to input value
The user inputalicewill be trimmed toalicebefore updatingusername.Final Answer:
User: alice -> Option AQuick Check:
.trimremoves spaces around input [OK]
- Assuming spaces remain in bound data
- Confusing trim with lazy or number
- Expecting trimming only on submit
<input v-model.number.lazy="score">
What is the main issue if the user types
42 but the bound score variable remains a string instead of a number?Solution
Step 1: Understand
The.lazyevent timing.lazymodifier updates the bound data only on the change event (when input loses focus), not on every input event.Step 2: Reason about
The.numberconversion timing.numbermodifier converts the value when the update happens. If the update hasn't happened yet (input still focused),scoreremains unchanged.Final Answer:
The input event is not triggered because .lazy waits for change event -> Option BQuick Check:
.lazydelays update, so number conversion waits [OK]
- Thinking .number fails with .lazy
- Believing variable type is fixed
- Assuming modifier order matters here
v-model usage?Solution
Step 1: Understand modifier order importance
Vue applies modifiers from left to right. The.lazymodifier must come first to delay update until change event.Step 2: Apply
After update triggers,.trimand.numberafter.lazy.trimremoves spaces, then.numberconverts to number before setting the variable.Final Answer:
<input v-model.lazy.trim.number="value"> -> Option CQuick Check:
Lazy first, then trim, then number [OK]
- Placing .lazy last so update happens too early
- Mixing modifier order causing wrong behavior
- Assuming order does not affect result
Ready to prove it?
Pass the test and this pattern moves unknown
