π§© 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 :
