Vue Single File Components (SFC) – Structure Your Vue Code Effectively (2025 Guide)
Introduction – What Are Single File Components in Vue?
Vue’s Single File Components (SFCs) allow developers to organize their template, script, and styles in a single .vue file. This encapsulation streamlines development, improves maintainability, and promotes reusability across Vue projects.
In this guide, you’ll learn:
- The anatomy of an SFC
- How to create and use SFCs
- Benefits of using SFCs in Vue
- Real-world best practices
What Is a .vue File?
A Single File Component is a .vue file that contains three optional sections:
<template> <!-- Markup for the component -->
<script> <!-- Component logic (data, methods, lifecycle) -->
<style> <!-- Scoped or global styling -->
Each section serves a clear purpose and keeps all component logic and presentation in one place.
Basic Example of a Vue SFC
HelloWorld.vue
<template>
<div class="greeting">Hello, {{ name }}!</div>
</template>
<script>
export default {
name: 'HelloWorld',
props: {
name: String
}
}
</script>
<style scoped>
.greeting {
color: blue;
font-size: 20px;
}
</style>
The scoped attribute ensures styles don’t leak outside the component.
Anatomy of a Vue SFC
| Section | Purpose |
|---|---|
<template> | The HTML markup of the component |
<script> | JavaScript logic: data, props, methods, hooks |
<style> | CSS/SCSS styling, optionally scoped |
You can also use:
<script setup>for Composition API<style lang="scss">for SCSS styling
Importing & Using SFCs
In App.vue
<template>
<HelloWorld name="Vue" />
</template>
<script>
import HelloWorld from './components/HelloWorld.vue';
export default {
components: {
HelloWorld
}
}
</script>
Using <script setup> (Composition API)
<template>
<h2>{{ message }}</h2>
</template>
<script setup>
import { ref } from 'vue';
const message = ref('Hello from setup!');
</script>
More concise and performant. Recommended for Vue 3 projects.
Real-World Use Case – Reusable Button
BaseButton.vue
<template>
<button :class="type" @click="$emit('click')">
<slot />
</button>
</template>
<script>
export default {
props: {
type: {
type: String,
default: 'primary'
}
}
}
</script>
<style scoped>
.primary {
background-color: blue;
color: white;
}
</style>
Use in Any Parent:
<BaseButton type="primary" @click="handleClick">Click Me</BaseButton>
Advanced Features of SFCs
| Feature | Benefit |
|---|---|
| Scoped Styles | Isolates styling per component |
| CSS Preprocessors | Use SCSS, LESS, PostCSS with lang attribute |
| Custom Blocks | Add blocks like <docs>, <i18n>, or <route> |
| Hot Module Replacement | Fast updates during development |
| Composition API | script setup supports modern composition logic |
Vue SFC – Best Practices
| Practice | Why It Helps |
|---|---|
| Use one component per file | Improves readability and reuse |
Use script setup where possible | Simplifies syntax and improves performance |
| Prefer scoped styles | Prevents CSS conflicts |
| Keep files under 200 lines | Easier to maintain |
| Group SFCs into feature folders | Better project structure |
Summary – Recap & Next Steps
Vue Single File Components provide a powerful, organized, and scalable way to build UI. They encapsulate structure, behavior, and style in one file, reducing the complexity of large apps.
Key Takeaways:
.vuefiles contain<template>,<script>, and<style>- Use
scopedto prevent style bleed - Import SFCs like modules in other components
- Use Composition API with
<script setup>for cleaner syntax
Real-World Relevance:
Used in component libraries, enterprise apps, and open-source projects where maintainability and modularity are critical.
FAQ Section
What is a Vue SFC?
It’s a .vue file that encapsulates HTML, JavaScript, and CSS for a Vue component.
What does scoped do in <style>?
It ensures the CSS styles only apply to the component they’re defined in.
Can I use SCSS or LESS in a .vue file?
Yes, use the lang attribute:
<style scoped lang="scss">
How is <script setup> different?
It simplifies code and improves performance by removing boilerplate in Composition API.
Are SFCs required to use Vue?
No, but they are recommended for structured, large-scale Vue applications.
Share Now :
