📖 Scenario: You are creating a small Vue app to show a greeting message. You will build a Single File Component (SFC) step-by-step.
🎯 Goal: Build a Vue Single File Component named Greeting.vue that displays a greeting message inside a <template> block, uses a message data property in the <script setup> block, and styles the message with a CSS class in the <style scoped> block.
📋 What You'll Learn
Create a Vue Single File Component named Greeting.vue
Add a <template> block with a <div> showing the greeting message
Add a <script setup> block defining a reactive message variable with the text 'Hello, Vue!'
Add a <style scoped> block that styles the greeting text color to blue
💡 Why This Matters
🌍 Real World
Single File Components are the standard way to build Vue apps. They keep HTML, JavaScript, and CSS together for each component, making code easier to manage and reuse.
💼 Career
Vue developers use SFCs daily to build interactive web apps. Understanding SFC structure is essential for frontend roles involving Vue.
Progress0 / 4 steps
1
Create the template block with a div
Create a <template> block in Greeting.vue containing a <div> element with the text {{ message }} inside it.
Vue
Hint
The <template> block holds the HTML structure. Use mustache syntax {{ message }} to show the message variable.
2
Add the script setup block with message variable
Add a <script setup> block below the template. Inside it, import ref from 'vue' and create a constant called message using ref with the initial value 'Hello, Vue!'.
Vue
Hint
Use import { ref } from 'vue' and then const message = ref('Hello, Vue!') to create a reactive variable.
3
Add scoped style block to color the message blue
Add a <style scoped> block below the script. Inside it, write CSS to make the div text color blue.
Vue
Hint
The scoped attribute limits styles to this component. Use div { color: blue; } to style the text.
4
Complete the Single File Component structure
Ensure the Greeting.vue file has the <template>, <script setup>, and <style scoped> blocks in this order, with the exact content from previous steps.
Vue
Hint
Check that all three blocks are present and in the correct order with the correct content.
Practice
(1/5)
1. What is the main purpose of a Vue Single File Component (SFC)?
easy
A. To write only JavaScript code without any HTML or CSS
B. To separate HTML, CSS, and JavaScript into different files
C. To keep template, script, and style in one file for better organization
D. To create components without any styling or templates
Solution
Step 1: Understand the structure of a Vue SFC
A Vue Single File Component combines template (HTML), script (JavaScript), and style (CSS) in one file for easier management.
Step 2: Compare options with this concept
To keep template, script, and style in one file for better organization correctly describes this purpose. Options B, C, and D contradict the idea of combining all parts in one file.
Final Answer:
To keep template, script, and style in one file for better organization -> Option C
Quick Check:
Single File Component = Combined template, script, style [OK]
Hint: SFC means all parts in one file for neatness [OK]
Common Mistakes:
Thinking SFC separates files instead of combining
Believing SFC is only JavaScript code
Ignoring the style section in SFC
2. Which of the following is the correct way to define the style section in a Vue Single File Component to apply styles only to this component?
easy
A.
B.
C.
D.
Solution
Step 1: Recall how scoped styles work in Vue SFC
Using the uses the correct 'scoped' attribute. Others like 'global', 'module', or 'isolated' are incorrect or unrelated here.
Final Answer:
<style scoped> ... </style> -> Option A
Quick Check:
Scoped style = <style scoped> [OK]
Hint: Use 'scoped' attribute to limit styles to component [OK]
Common Mistakes:
Using <style> without scoped causes global styles
Confusing 'module' with 'scoped' attribute
Trying non-existent attributes like 'isolated'
3. Given this Vue SFC snippet, what will be displayed when the component renders?
The template uses {{ message }} which displays the value of the 'message' variable from the script.
Step 2: Check the script setup variable
The script setup defines 'message' as 'Hello Vue!'. This value will replace {{ message }} in the template.
Final Answer:
Hello Vue! -> Option B
Quick Check:
Template interpolation shows variable value [OK]
Hint: Variables in script setup show in template with {{ }} [OK]
Common Mistakes:
Thinking {{ message }} prints literally
Assuming message is undefined without export
Confusing script setup with normal script
4. Identify the error in this Vue Single File Component:
<template>
<button @click='increment'>Count: {{ count }}</button>
</template>
<script setup>
let count = 0
function increment() {
count++
}
</script>
medium
A. The script setup block cannot contain functions
B. The increment function is missing parentheses in the template
C. The template syntax for event binding is incorrect
D. The count variable should be a ref to be reactive
Solution
Step 1: Check reactivity of count variable
In Vue 3 script setup, to make a variable reactive and update the template, it must be a ref or reactive object.
Step 2: Analyze the code
Here, count is a plain number, so incrementing it won't update the template. It should be defined as const count = ref(0).
Final Answer:
The count variable should be a ref to be reactive -> Option D
Quick Check:
Reactive state needs ref() or reactive() [OK]
Hint: Use ref() for reactive variables in script setup [OK]
Common Mistakes:
Forgetting to import ref from 'vue'
Thinking functions can't be in script setup
Misusing event binding syntax
5. You want to create a reusable Vue Single File Component that accepts a title prop and displays it inside an <h1> tag. Which of the following is the correct way to define the prop and use it in the template?
hard
A.
{{ title }}
B.
{{ props.title }}
C.
{{ title }}
D.
{{ title }}
Solution
Step 1: Understand how to define props in script setup
In Vue 3 script setup, props are defined using defineProps(), which returns an object with prop values.
Step 2: Check how to use props in template
Since defineProps returns an object named props, you can destructure or use the props object. But Vue automatically exposes props keys as variables, so {{ title }} works if you assign props to a variable.
Step 3: Analyze options
{{ title }}
correctly defines props and uses {{ title }} in template.
{{ props.title }}
incorrectly uses {{ props.title }} in template (props is not exposed automatically).
{{ title }}
wrongly assigns defineProps to title.
{{ title }}
does not use defineProps.
Final Answer:
Option A correctly defines and uses the title prop -> Option A
Quick Check:
Use defineProps() and {{ title }} for props [OK]
Hint: Use defineProps() and {{ title }} to access props [OK]
Common Mistakes:
Using props.title in template without destructuring
Assigning defineProps() result to wrong variable
Not using defineProps() at all
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.