Props — Data Flowing Down
BookCard still shows "Dune" every time, because the title is written inside it. A card that can only show one book is useless. The list knows which books exist; the card should just display whatever book it is given. Props are how a parent hands data to a child.
The rule
Props flow down, and the child only reads them.
The child declares which props it accepts with defineProps. It is a compiler macro, so there is nothing to import:
<!-- BookCard.vue -->
<script setup>
const props = defineProps({
id: { type: Number, required: true },
title: { type: String, required: true },
pages: Number,
read: { type: Boolean, default: false }
})
</script>
<template>
<article class="card">
<h3>{{ title }}</h3>
<p>{{ pages }} pages</p>
</article>
</template>The parent passes values as attributes:
<BookCard :id="1" title="Dune" :pages="412" :read="true" />Look closely at the colons. title="Dune" passes the literal string Dune. :pages="412" means "evaluate 412 as JavaScript", so the child receives the number 412, not the text "412". Without the colon, pages="412" would pass a string and fail the Number type check with a warning. Any time a prop is not a fixed string, use the colon: :title="book.title".
In the template, props are used by name ({{ title }}). In script code, read them through the object: props.title.
One-way data flow: when the parent's value changes, the new value flows down into the child. But the child must never assign to a prop. props.pages = 0 triggers a warning and the parent's data does not change. The parent owns the data; the child borrows it. (How a child asks the parent for a change is the next lesson.)