Component Props
Props pass data from a parent component down to a child component. A prop is a named value the parent hands to the child.
Declaring a prop
The props list names each value the child accepts.
const Label = defineComponent({
props: ['label'],
template: '<p>{{ label }}</p>'
})The script setup form
A child written with script setup declares props with defineProps.
const props = defineProps(['label'])Passing a prop
v-bind hands the greeting value to the child.
<Label :label="greeting" />A fixed string
A plain attribute passes a fixed string.
<Label label="Good morning" />Example
<script setup>
import { defineComponent } from 'vue'
const greeting = 'Hello'
const Label = defineComponent({
props: ['label'],
template: '<p>{{ label }}</p>'
})
</script>
<template>
<div>
<Label :label="greeting" />
</div>
</template>
Key points
- The child declares each prop it accepts.
- The parent passes a prop with v-bind, like :label.
- The child reads the prop inside its template.
- A prop can carry text, a number, or a variable.
- Props flow one way, from parent down to child.
In this example, greeting holds the string Hello. Label declares a prop named label. The parent passes greeting to Label with v-bind. Label shows the prop value in its paragraph.
Note
A child never changes its own prop value.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
How does a parent pass data to a child?