In the components lesson, placing <StallHeader /> twice gave you two copies that were exactly alike. For menu cards, copies that are alike are not enough. The Fried Rice card and the Iced Tea card have the same shape, but different names and prices.
Props fill in that difference. Props are data a parent sends to a child through attributes on the child's tag. Picture the price-label holders in the display case at Mrs. Sri's stall. Every holder has the same shape, but Mrs. Sri slides a different slip into each one: "Fried Rice, $15", "Iced Tea, $5". The holder only shows what's on its slip. If a price has to change, Mrs. Sri writes a new slip; the holder doesn't. In Vue, the holder is the child component, the slip is the props, and Mrs. Sri is the parent.
The parent sends, the child receives
The child declares which props it takes, with their types, using defineProps:
<!-- MenuCard.vue -->
<script setup>
const props = defineProps({
name: String,
price: Number,
stock: Number,
})
</script>
<template>
<div class="card">
<h3>{{ name }}</h3>
<p>${{ price }}</p>
<small>Stock: {{ stock }}</small>
</div>
</template>
The parent fills those props in through attributes. Each <MenuCard> tag becomes one copy of the card:
<!-- App.vue -->
<script setup>
import MenuCard from "./MenuCard.vue"
</script>
<template>
<MenuCard name="Fried Rice" :price="15" :stock="8" />
<MenuCard name="Chicken Noodles" :price="12" :stock="0" />
<MenuCard name="Iced Tea" :price="5" :stock="20" />
</template>
| Tag in App.vue | What shows |
|---|---|
name="Fried Rice" :price="15" :stock="8" |
Fried Rice, $15, Stock: 8 |
name="Chicken Noodles" :price="12" :stock="0" |
Chicken Noodles, $12, Stock: 0 |
name="Iced Tea" :price="5" :stock="20" |
Iced Tea, $5, Stock: 20 |
definePropsis a compiler macro: an instruction Vue itself recognises in<script setup>, so it needs no import.- In the template, props are read directly:
{{ name }}. In the script, props are read through the objectdefinePropsreturns:props.name. - So one component gives three cards with different data. That's what props are for.
The colon: text or a JavaScript value
name="Fried Rice"with no colon sends text as written.:price="15"is short forv-bind:price. Its content runs as JavaScript, so the child gets the number 15. The name of a parent's ref works too::stock="riceStock".price="15"with no colon sends the text"15", even though it looks like a number.
Take a guess: MenuCard declares price: Number and shows Takeaway: ${{ price + 2 }}. If the parent writes <MenuCard name="Fried Rice" price="15" />, with no colon, what shows?
The card says Takeaway: $152. Text plus a number in JavaScript means joining, not adding: "15" + 2 becomes "152". With :price="15", it says Takeaway: $17.
In development mode, the console tells you what's wrong: [Vue warn]: Invalid prop: type check failed for prop "price". Expected Number with value 15, got String with value "15". The exercise editor uses the production build, so warnings like this don't appear there. The odd number is your only clue.
Type, required and default
Each prop can be given rules, written as an object:
const props = defineProps({
name: { type: String, required: true },
price: { type: Number, required: true },
stock: { type: Number, default: 0 },
notes: { type: Array, default: () => [] },
})
typeis the expected type:String,Number,Boolean,Array,Object,Function.required: truemeans the parent must send it. If the parent forgets it, the card still shows with an emptyname. In development mode, the console writes[Vue warn]: Missing required prop: "name".defaultis used when the parent doesn't send that prop.<MenuCard name="Iced Tea" :price="5" />showsStock: 0.- An array or object default is written as a function,
() => [], so each copy gets a new array.default: []makes every copy share one and the same array. - Since Vue 3.5, props may be destructured with a default value,
const { stock = 0 } = defineProps({ stock: Number }), andstockstays reactive.watch(a function that runs code when data changes) has its own lesson later. Forwatch, write a destructured prop as a getter,watch(() => stock, ...). The compiler rejectswatch(stock, ...)with"stock" is a destructured prop and should not be passed directly to watch().
Props are read-only
Data flows one way: from parent to child. The child may read props, but never change them. Here is the wrong way, a button in the child that tries to lower the prop stock:
<!-- MenuCard.vue, the wrong way -->
<button @click="stock--">Buy 1</button>
- Clicking that button changes nothing. In a
.vuefile, in development mode, the console writes[Vue warn] Set operation on key "stock" failed: target is readonly., the same message as forprops.stock--in the script. If the template is compiled in the browser by the development build (vue.global.js), the message is[Vue warn]: Attempting to mutate prop "stock". Props are readonly.The exercise editor loads the production build (vue.global.prod.js), so there the number just stays the same, with no message in the console. - The stock belongs to the parent, so the parent changes it. As soon as the parent's
stockref changes, the new number flows down to the child. - A child that needs to tell the parent "someone bought one" sends an event to the parent. How to do it is in the next lesson, Emits.
- If the parent puts
v-modelon the child (for example a portion count changed with + and - buttons), the child still doesn't change its prop. The child usesdefineModel(), which sends an event to the parent. How to do it is in the Emits lesson. - If the child only needs a starting value and then manages it itself, copy that value into a ref the child owns:
const remaining = ref(props.stock).
📌 Two-word prop names
In the script, a prop name is written camelCase: promoPrice. In the parent's template, it's usually written kebab-case: <MenuCard :promo-price="13" />. Vue treats promo-price and promoPrice as the same prop, so both work.
.vue files and the exercise editor
The exercise editor runs one component: the script, the ---template--- line, then the template. In the editor, a child is written as an object in the script, and placed with <component :is>:
const MenuCard = {
props: {
name: String,
price: Number,
stock: Number,
},
template: `
<div class="card">
<h3>{{ name }}</h3>
<p>{{ "$" + price }}</p>
<small>Stock: {{ stock }}</small>
</div>
`,
}
---template---
<div>
<component :is="MenuCard" name="Fried Rice" :price="15" :stock="8" />
<component :is="MenuCard" name="Iced Tea" :price="5" :stock="20" />
</div>
| In a .vue file | In the exercise editor |
|---|---|
defineProps({ ... }) in MenuCard.vue |
the props: { ... } option in the MenuCard object |
the <template> of MenuCard.vue |
the template option, as text between backticks |
import MenuCard from "./MenuCard.vue" then <MenuCard ... /> |
<component :is="MenuCard" ... /> |
- The editor can't register component names, so
<MenuCard />there only becomes an empty<menucard>tag.<component :is="MenuCard">is Vue's way of showing a component that is kept in a variable. definePropsonly works in the<script setup>of a.vuefile. In the editor,definePropsreceives nothing.- Between backticks,
${starts JavaScript, so the price is written{{ "$" + price }}there, not${{ price }}. - This lesson's exercises use this editor form:
ProductCardandProductItemdeclareprops, and the parent sends the values with:title="title"or:name="p.name". The checker reads the resulting HTML, the child'spropsoption, and the attributes the parent sends.
⚠️ Why is the price weird, or the button doing nothing?
- Forgetting the colon for a number.
price="15"sends text. Use:price="15". - Changing a prop in the child. Nothing changes. Change the data in the parent, or send the parent an event.
- An array or object default without a function. Write
default: () => [], notdefault: []. - Typing the value into the template again. In the exercise, the checker makes sure that
<h2 id="title">is filled with{{ title }}and<span id="price">with{{ price }}in ProductCard's template, not the textLaptoptyped in directly.
TL;DR: Props are data a parent sends to a child. The child declares them with defineProps({ name: String, price: Number }), then the parent sends them through attributes on the child's tag: <MenuCard name="Fried Rice" :price="15" />. Without a colon, you send text. With a colon, you send a JavaScript value, such as a number. type, required and default set the rules for each prop, and an array or object default is written as a function. Props are read-only: data flows one way, and the parent makes the changes. In the exercise editor, a child is written as an object with the props and template options, then placed with <component :is="MenuCard">.
⚡ The child declares its props
Write:
<!-- MenuCard.vue -->
<script setup>
const props = defineProps({
name: String,
price: Number,
stock: Number,
})
</script>
Meaning: Props are data from a parent to a child, sent through attributes on the child's tag. MenuCard.vue here is a child that takes three props. defineProps is a compiler macro (an instruction Vue itself recognises), so it needs no import. In the template, props are read directly, {{ name }}. In the script, through props.name.
⚡ The parent sends them through attributes
Write:
<MenuCard name="Fried Rice" :price="15" :stock="8" />
<MenuCard name="Iced Tea" :price="5" :stock="20" />
Guess first: MenuCard shows Takeaway: ${{ price + 2 }}. If the parent writes price="15" with no colon, what shows?
Takeaway: $152. Without the colon, the text "15" is sent, and in JavaScript text plus a number means joining.
Meaning: The parent places two copies of MenuCard, each with its own data. Without a colon, you send text. With a colon (:price), you send a JavaScript value, here the number 15.
⚡ Props are read-only
Guess first: the child writes props.name = "Budi". Does the name change?
No. Props are read-only (they can only be read), so the change is refused.
Meaning: Data flows one way, from parent to child. The parent is the one that changes the data. If the child needs to tell the parent something, it sends an event, which is the topic of the next lesson, Emits.