Vue3 Fundamentals · Components, Props & Events

defineModel

defineModel

Building v-model by hand means writing the same prop and event pair on every component. Vue gives you a macro that writes both for you.

defineModel() declares a modelValue prop and an update:modelValue event in one call, and returns a writable ref:

<script setup>
const model = defineModel();
</script>

<template>
  <input v-model="model" />
</template>

Reading model gives the current value; assigning to it emits update:modelValue. Because v-model on the input already reads and writes a ref, you can bind the model straight to it.

The macro is the same convention you built by hand — modelValue in, update:modelValue out — so a parent uses it identically: <Field v-model="name" />. The difference is that defineModel keeps the prop and the event in sync for you, so you write one line instead of two.

defineModel is a compiler macro; you never import it. It is available in Vue 3.4 and later. The Test sets the input's value and checks the component emitted the new value.

Challenge

The input still cannot send its value back. Replace the manual prop and event with the macro: export an App component from App.vue that calls defineModel() and binds the returned model to the input with v-model.

The Test mounts your component with modelValue set to Vue, types Vue 3 into the input, and checks the component emitted update:modelValue with Vue 3. The starter only reads the prop, so nothing is emitted and the Test fails.

App.vue
<script setup>
defineProps(['modelValue']);
</script>

<template>
  <input :value="modelValue" />
</template>

No test run yet

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