📖 Scenario: You are building a simple Vue app that shows a greeting message. The parent component holds the message text. The child component will receive this message as a prop and display it.
🎯 Goal: Create a parent component that passes a greeting message to a child component using props. The child component should display the message inside a paragraph.
📋 What You'll Learn
Create a parent component named ParentComponent with a data property greeting set to 'Hello from Parent!'.
Create a child component named ChildComponent that accepts a prop named message.
Use the ChildComponent inside ParentComponent and pass the greeting data as the message prop.
Display the message prop inside a <p> tag in ChildComponent.
💡 Why This Matters
🌍 Real World
Passing data from parent to child components is a common pattern in Vue apps to keep components reusable and organized.
💼 Career
Understanding props is essential for building interactive Vue applications and collaborating in frontend development teams.
Progress0 / 4 steps
1
Set up the parent component data
Create a Vue component named ParentComponent with a data function that returns an object containing a property greeting set to the string 'Hello from Parent!'.
Vue
Hint
Remember, the data option in Vue components is a function that returns an object with your data properties.
2
Create the child component with a prop
Create a Vue component named ChildComponent that declares a prop named message.
Vue
Hint
Use the props option to declare props in Vue components as an array of strings.
3
Use the child component in the parent and pass the prop
Inside ParentComponent, register ChildComponent in the components option. Then, in the template, use <ChildComponent :message="greeting" /> to pass the greeting data as the message prop.
Vue
Hint
Remember to register the child component in the components option and use the :message="greeting" binding in the template.
4
Display the prop message in the child component template
Add a template section to ChildComponent that displays the message prop inside a <p> tag.
Vue
Hint
Use the template property in the child component object to define the HTML that uses the message prop.
Practice
(1/5)
1. What is the main purpose of props in Vue components?
easy
A. To pass data from a parent component to a child component
B. To store local state inside a component
C. To handle user input events
D. To define methods inside a component
Solution
Step 1: Understand the role of props
Props are designed to send data from a parent component down to its child components.
Step 2: Differentiate props from other features
Local state stores data inside a component, events handle user actions, and methods define functions. Only props pass data downward.
Final Answer:
To pass data from a parent component to a child component -> Option A
Quick Check:
Props = Parent to Child Data [OK]
Hint: Props always send data from parent to child [OK]
Common Mistakes:
Confusing props with local state
Thinking props handle events
Assuming props define methods
2. Which of the following is the correct way to declare a prop named title in a Vue 3 child component using <script setup>?
easy
A. const props = defineProps({ title: String })
B. props: ['title']
C. export default { props: ['title'] }
D. const title = ref('')
Solution
Step 1: Identify Vue 3 <script setup> syntax
In Vue 3 with <script setup>, props are declared using defineProps().
Step 2: Match the correct syntax for prop declaration
const props = defineProps({ title: String }) correctly declares a prop named title of type String.
Final Answer:
const props = defineProps({ title: String }) -> Option A
A. Props must be declared as reactive refs to modify
B. The prop type Number is invalid in defineProps
C. Missing import of defineProps
D. Props are read-only and cannot be assigned new values
Solution
Step 1: Recall prop immutability
Props in Vue are read-only inside child components and cannot be directly modified.
Step 2: Identify the error in code
The code tries to assign a new value to props.count, which violates the read-only rule and causes an error.
Final Answer:
Props are read-only and cannot be assigned new values -> Option D
Quick Check:
Props cannot be changed inside child [OK]
Hint: Never assign new values to props inside child [OK]
Common Mistakes:
Trying to mutate props directly
Confusing props with reactive state
Ignoring Vue warnings about prop mutation
5. You want to pass a list of user names from a parent to a child component and display them. Which approach correctly passes and uses the prop in the child component using Vue 3 <script setup>?
Parent template:
<UserList :users="['Alice', 'Bob', 'Charlie']" />
Choose the correct child component code:
hard
A.
<script setup>
export default {
props: ['users']
}
</script>
<template>
<ul>
<li v-for="user in users" :key="user">{{ user }}</li>
</ul>
</template>
B.
<script setup>
const users = ref([])
</script>
<template>
<ul>
<li v-for="user in users" :key="user">{{ user }}</li>
</ul>
</template>