Skip to content
\n\n","url":"https://ez4code.com/snippets/vue3-props-emits","keywords":"props, emits, v-model","author":{"@type":"Person","name":"EZ4Code Team"},"publisher":{"@type":"Organization","name":"EZ4Code","logo":{"@type":"ImageObject","url":"https://ez4code.com/logo.png"}},"datePublished":"2024-01-01","dateModified":"2026-08-01","image":"https://ez4code.com/og-image.png"}
Vue 3

Props & Emits

Declare inputs and events, and implement v-model on a component.

By EZ4Code Team
propsemitsv-model

Code

<script setup>
const props = defineProps({
  modelValue: { type: String, required: true }
});
const emit = defineEmits(["update:modelValue", "submit"]);

function update(value) {
  emit("update:modelValue", value);
}
</script>

<template>
  <input
    :value="props.modelValue"
    @input="update($event.target.value)"
  />
  <button @click="emit('submit')">Save</button>
</template>

Explanation

defineProps declares a component's inputs with type validation, while defineEmits declares the events it can fire. Together with update:modelValue they implement v-model on a custom component. Both are compile-time macros, so they need no import.

More Vue 3 Snippets