📖 Scenario: You are building a simple webpage that shows a greeting message using Vue components. Components help you organize your webpage into small, reusable parts.
🎯 Goal: Create a Vue component named GreetingMessage that displays the text "Hello, Vue learner!" and use it inside the main app.
📋 What You'll Learn
Create a Vue component named GreetingMessage using the <script setup> syntax.
The component should render a <p> tag with the exact text: "Hello, Vue learner!".
Use the GreetingMessage component inside the main App component template.
Use the Composition API with <script setup> and the ref or reactive API if needed.
💡 Why This Matters
🌍 Real World
Vue components let you build webpages by breaking the UI into small, reusable parts. This makes your code easier to manage and reuse.
💼 Career
Knowing how to define and use components is essential for frontend development jobs using Vue.js, a popular framework for building interactive web apps.
Progress0 / 4 steps
1
Create the GreetingMessage component
Create a Vue component named GreetingMessage using <script setup> that renders a <p> tag with the text "Hello, Vue learner!".
Vue
Hint
Use the <template> tag to write the HTML and <script setup> for the component script.
2
Set up the main App component
Create the main App component with a <template> and <script setup> tags. Import the GreetingMessage component from the file ./GreetingMessage.vue.
Vue
Hint
Use import GreetingMessage from './GreetingMessage.vue' and include <GreetingMessage /> in the template.
3
Register the GreetingMessage component
In the App component, ensure the GreetingMessage component is properly imported and used inside the <template> section as a self-closing tag.
Vue
Hint
Make sure the component tag is exactly <GreetingMessage /> and the import path is correct.
4
Complete the Vue app setup
Add the export default statement to the App component if needed, or confirm the component is ready to be used as the root component in a Vue app.
Vue
Hint
With <script setup>, you do not need to write export default. Just ensure the component is correctly structured.
Practice
(1/5)
1. What is the main purpose of defining components in Vue?
easy
A. To handle server-side rendering only
B. To create reusable pieces of UI that can be used throughout the app
C. To connect the app to a database
D. To write CSS styles for the app
Solution
Step 1: Understand what components do in Vue
Components are small, reusable parts of the user interface that can be used multiple times in an app.
Step 2: Identify the correct purpose
Among the options, only creating reusable UI pieces matches the role of components.
Final Answer:
To create reusable pieces of UI that can be used throughout the app -> Option B
Quick Check:
Components = reusable UI blocks [OK]
Hint: Components build reusable UI parts in Vue apps [OK]
Common Mistakes:
Confusing components with styling or backend logic
Thinking components handle database connections
Believing components only work on the server side
2. Which of the following is the correct way to start a Vue component using the Composition API with <script setup>?
easy
A.
{{ message }}
B.
{{ message }}
C.
{{ message }}
D.
{{ message }}
Solution
Step 1: Recognize the <script setup> syntax
In Vue 3, <script setup> is a special syntax that allows direct use of variables without export default.
Step 2: Check which option uses <script setup> correctly
<script setup>
const message = 'Hello'
</script>
<template>
<p>{{ message }}</p>
</template> declares a variable inside <script setup> and uses it in the template, which is correct.
D. Using toUpperCase() on a possibly undefined prop
Solution
Step 1: Analyze prop definition and usage
defineProps({ title: String }) declares title without required: true or default value, so props.title can be undefined.
Step 2: Locate the error in JS code
In showTitle(), props.title.toUpperCase() assumes title is a string, but calling toUpperCase() on undefined causes a runtime error.
Final Answer:
Using toUpperCase() on a possibly undefined prop -> Option D
Quick Check:
Validate props before string operations [OK]
Hint: Guard undefined props before methods like toUpperCase [OK]
Common Mistakes:
Applying string methods to props that may be undefined
Forgetting to import defineProps (though auto-imported in some setups)
Assuming props are reactive variables directly
5. You want to create a Vue component that accepts a user prop object with name and age, and displays "Name: [name], Age: [age]". Which is the best way to define and use this prop in a <script setup> component?