Vue3 Fundamentals ยท Events & Forms

Shaping the bound value

v-model modifiers

Shaping the bound value

The value a field hands you is not always the value you want to store. A user leaves spaces around a name; a number field still gives you a string; a search box fires on every keystroke when you only care about the finished word. You could clean each value in a handler, but Vue lets you say how in the template, with a modifier.

A modifier is a dot-suffixed word on v-model:

<input v-model.trim="name" />
<input v-model.number="age" type="number" />
<input v-model.lazy="query" />
  • .trim removes leading and trailing whitespace before storing the value.
  • .number casts the value to a number, so age holds 30, not '30'.
  • .lazy waits for the change event instead of input, so it updates when the field loses focus rather than on every keystroke.

The modifier changes what lands in your ref, not what the user typed. With .trim, typing spaces around a word still stores the trimmed word.

Your job is to trim the value a field stores in App.vue.

Challenge

The name field keeps the spaces the user types around it. Export an App component from App.vue that renders a text <input> bound with v-model and a <p> wrapped in square brackets showing the stored value.

The Test types Vue and expects the paragraph to read [Vue], with the spaces gone. The starter binds without a modifier, so the spaces survive and the Test fails.

App.vue
<script setup>
import { ref } from 'vue';

const name = ref('');
</script>

<template>
  <input v-model="name" />
  <p>[{{ name }}]</p>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge โ€” you only see the result, never the test source.