Passing Props
Part of the Fundamentals section of Coddy's Vue journey. Lesson 33 of 55.
Props pass values from parent to child. This parent sends a title and a number to BookCard; the child declares them with defineProps and reads them in its template.
// Child script setup:
defineProps({ title: String, pages: Number });
// Child template: <p>{{ title }}: {{ pages }} pages</p>
// Parent template: <BookCard title="Stories" :pages="120" />A colon evaluates an expression, so :pages="120" is a number while pages="120" is a string. Props flow down when the parent changes them. Treat them as read-only in the child; do not copy them into local state if the display must always follow the parent.
Challenge
MediumCreate ShiftCard.vue with label and numeric places props. Render the label in an h2 and places followed by places in a paragraph. In #morning, pass Morning shift and a parent ref starting at 6. Button #grow increments that ref and the child must follow.
Try it yourself
<!doctype html><html><head><meta charset="UTF-8"><title>Vue practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
7Reusable Components
Importing ComponentsIndependent InstancesPassing PropsProp Types and DefaultsRecap: Component Cards2Reactive State
Reactive RefsUpdating Refs in ScriptEvent Handler ArgumentsReactive ObjectsRecap: Reactive State5Conditions and Lists
Conditional BranchesShow without UnmountingRendering ListsStable List IdentityRecap: Conditions8Component Communication
Emitting EventsEvent PayloadsLifting Shared StateDefault and Named SlotsRecap: Shared Panel11Project: Community Board
Board StructureAdding Community Tasks