Vue Props
A component that always shows the same thing is not very useful. Props (short for properties) let a parent pass data down into a child component, just like attributes on an HTML element: <product-card name="Laptop" :price="999">.
Declaring and passing props
A component declares which props it accepts in the props option. Inside the component, props are available in the template and as this.propName (Options API) or props.propName (setup(props)).
<style>
.card { display: inline-block; width: 140px; margin: 6px; padding: 10px; border: 1px solid #ddd; border-radius: 8px; text-align: center; }
.price { color: #42b883; font-weight: bold; }
</style>
<div id="app">
<product-card name="Headphones" :price="79" emoji="🎧"></product-card>
<product-card name="Keyboard" :price="49.5" emoji="⌨️"></product-card>
<product-card v-for="p in extra" :key="p.name" :name="p.name" :price="p.price" :emoji="p.emoji"></product-card>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({
data: () => ({ extra: [{ name: 'Mouse', price: 25, emoji: '🖱️' }] })
});
app.component('product-card', {
props: ['name', 'price', 'emoji'],
template: `
<div class="card">
<div style="font-size:32px">{{ emoji }}</div>
<div>{{ name }}</div>
<div class="price">\${{ price.toFixed(2) }}</div>
</div>`
});
app.mount('#app');
</script>Note:
name="Headphones"passes a string. To pass a number, boolean, array, object or a variable, usev-bind::price="79". Without the colon,price="79"would be the string"79"and.toFixed()would fail.
(The \$ in the template string escapes $ so JavaScript doesn't treat ${ as a placeholder.)
Prop validation
Instead of an array of names, declare props as an object with types, defaults and rules. Vue warns in the console (in development builds) when a parent passes something invalid.
props: {
title: { type: String, required: true },
count: { type: Number, default: 0 },
tags: { type: Array, default: () => [] }, // objects/arrays need a factory
size: {
type: String,
default: 'md',
validator: (v) => ['sm', 'md', 'lg'].includes(v)
},
rounded: Boolean
}| Option | Purpose |
|---|---|
type |
String, Number, Boolean, Array, Object, Function, Date, a class, or an array of these |
required |
Warn if the prop is missing |
default |
Value used when the prop is absent (use a function for arrays/objects) |
validator |
Custom check returning true / false |
Defaults and boolean props
A Boolean prop behaves like an HTML boolean attribute: just writing its name means true.
<style>
.btn { border: none; padding: 6px 12px; margin: 4px; border-radius: 4px; color: white; background: #42b883; }
.sm { font-size: 12px; } .lg { font-size: 20px; }
.outline { background: white; color: #42b883; border: 2px solid #42b883; }
</style>
<div id="app">
<base-button>Default</base-button>
<base-button size="lg">Large</base-button>
<base-button size="sm" outline>Small outline</base-button>
<base-button outline disabled>Disabled</base-button>
</div>
<script type="module">
import { createApp } from 'vue';
const app = createApp({});
app.component('base-button', {
props: {
size: { type: String, default: 'md', validator: v => ['sm', 'md', 'lg'].includes(v) },
outline: Boolean,
disabled: Boolean
},
template: `
<button class="btn" :class="[size, { outline }]" :disabled="disabled">
<slot></slot>
</button>`
});
app.mount('#app');
</script><slot> puts the content between the component's tags into the button — you will learn slots in their own lesson.
camelCase vs kebab-case
Declare props in camelCase (initialCount) because that is a valid JavaScript identifier. In in-DOM templates, pass them in kebab-case (:initial-count="5"); Vue maps one to the other. In string templates and SFCs either form works.
One-way data flow
Props flow down only. When the parent's value changes, the child receives the new value — but the child must not change a prop itself. If it tries, Vue warns and the change is ignored, because otherwise it would be unclear who owns the data.
If a child needs a local, changeable copy, initialise its own state from the prop:
<div id="app">
<p>Parent's start value: {{ start }} <button @click="start += 10">+10 in parent</button></p>
<step-counter :initial-count="start"></step-counter>
</div>
<script type="module">
import { createApp, ref, computed } from 'vue';
const app = createApp({ data: () => ({ start: 5 }) });
app.component('step-counter', {
props: { initialCount: { type: Number, default: 0 } },
setup(props) {
const count = ref(props.initialCount); // local copy: starts from the prop
const doubled = computed(() => props.initialCount * 2); // derived: follows the prop
return { count, doubled };
},
template: `
<p>Child count: {{ count }} <button @click="count++">+1 in child</button></p>
<p>Child sees initialCount × 2 = {{ doubled }}</p>`
});
app.mount('#app');
</script>Click +10 in parent: doubled follows the prop, but count was only initialised from it. To send changes back up to the parent, the child emits an event — next lesson.
Tip: In
setup(props), don't destructure props (const { initialCount } = props) in a plainsetup()function — you would lose reactivity. Useprops.initialCount,toRefs(props), orcomputed.
Test yourself
How do you pass the number 3 (not the string "3") to a prop called max?
max="3":max="3"max={3}@max="3"
Why should an array prop's default be written as default: () => []?
- Arrays are not allowed as defaults
- So each instance gets its own new array instead of sharing one
- Because defaults must be async
- It makes the prop required
A child wants to change a value it received as a prop. What is the correct approach?
- Assign to the prop directly
- Use
v-html - Emit an event so the parent changes it (or keep a local copy)
- Delete the prop