Single File Components let you keep your HTML, JavaScript, and CSS for a Vue component all in one file. This makes your code neat and easy to manage.
Single File Components concept in Vue
<template> <!-- HTML goes here --> </template> <script setup> // JavaScript goes here </script> <style scoped> /* CSS goes here */ </style>
The <template> block holds the HTML structure.
The <script setup> block contains the JavaScript logic using Vue's Composition API.
<template> <button @click="count++">Clicked {{ count }} times</button> </template> <script setup> import { ref } from 'vue' const count = ref(0) </script> <style scoped> button { font-size: 1.2rem; padding: 0.5rem 1rem; } </style>
<template>
<h1>{{ title }}</h1>
</template>
<script setup>
const title = 'Hello Vue Single File Component'
</script>
<style scoped>
h1 {
color: teal;
}
</style>This component lets the user type their name. It updates the greeting below as they type. The input has an accessible label, and styles apply only inside this component.
<template>
<div>
<input v-model="name" placeholder="Enter your name" aria-label="Name input" />
<p>Hello, {{ name || 'stranger' }}!</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const name = ref('')
</script>
<style scoped>
div {
max-width: 300px;
margin: 1rem auto;
font-family: Arial, sans-serif;
}
input {
width: 100%;
padding: 0.5rem;
font-size: 1rem;
border: 1px solid #ccc;
border-radius: 4px;
}
p {
margin-top: 0.5rem;
font-size: 1.2rem;
color: #333;
}
</style>Use scoped on the style tag to keep CSS inside the component only.
The <script setup> syntax is simpler and recommended for Vue 3.2+.
Single File Components work best with build tools like Vite or Vue CLI.
Single File Components keep HTML, JS, and CSS together for easier coding.
They help organize your app into small, reusable parts.
Scoped styles prevent your CSS from affecting other parts of the app.
Practice
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 CQuick Check:
Single File Component = Combined template, script, style [OK]
- Thinking SFC separates files instead of combining
- Believing SFC is only JavaScript code
- Ignoring the style section in SFC
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 AQuick Check:
Scoped style = <style scoped> [OK]
- Using <style> without scoped causes global styles
- Confusing 'module' with 'scoped' attribute
- Trying non-existent attributes like 'isolated'
<template>
<div>{{ message }}</div>
</template>
<script setup>
const message = 'Hello Vue!'
</script>Solution
Step 1: Understand template interpolation
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 BQuick Check:
Template interpolation shows variable value [OK]
- Thinking {{ message }} prints literally
- Assuming message is undefined without export
- Confusing script setup with normal script
<template>
<button @click='increment'>Count: {{ count }}</button>
</template>
<script setup>
let count = 0
function increment() {
count++
}
</script>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 DQuick Check:
Reactive state needs ref() or reactive() [OK]
- Forgetting to import ref from 'vue'
- Thinking functions can't be in script setup
- Misusing event binding syntax
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?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 AQuick Check:
Use defineProps() and {{ title }} for props [OK]
- Using props.title in template without destructuring
- Assigning defineProps() result to wrong variable
- Not using defineProps() at all
Ready to prove it?
Pass the test and this pattern moves unknown
