Vue Single-File Components (SFCs)
Vue's Single-File Components (SFCs) bundle a component's template, logic, and styles into one .vue file. A build tool compiles this into browser-readable JS and CSS, turning your template into optimized render functions.
Why it exists
Before SFCs, a single component's concerns—its HTML structure, its JavaScript logic, and its CSS styles—were often split across multiple files. This made components harder to manage and reason about. SFCs were created to co-locate all related code for a component into one cohesive file.
The mental model
A Vue Single-File Component is a custom file format (.vue) that acts as a self-contained blueprint for a piece of your UI. It bundles the three core aspects of a component—structure (<template>), logic (<script>), and presentation (<style>)—into one organized file. Think of it as a complete recipe card for an interface element.
How it works
A .vue file is not understood by browsers. It requires a build tool like Vite or Vue CLI, which uses @vue/compiler-dom to parse the file. The compiler processes each language block separately. The <template> block is pre-compiled into highly efficient JavaScript render functions. The <script> block is treated as an ES Module that exports the component's definition. The <style> block is processed into CSS, which can be automatically scoped to the component to prevent styles from leaking.
When to use it
SFCs are the standard and recommended way to write components in any Vue project that has a build step. They are ideal for creating reusable, maintainable, and well-organized UI elements. You can enhance blocks with pre-processors by adding a lang attribute, for example, <script lang="ts"> for TypeScript or <style lang="scss"> for Sass.
When not to use it
Avoid SFCs if you are using Vue without a build step, such as including it from a CDN with a single <script> tag. In that scenario, you define components as plain JavaScript objects. SFCs are specifically designed for a development environment with a compilation pipeline.
One canonical example
A file named UserProfile.vue contains a <template> with the user's profile markup, a <script setup> block to fetch user data, and a <style scoped> block to style the profile card. The build tool compiles this into a single JavaScript module. Because the filename is UserProfile.vue, you can recursively reference it within its own template as <UserProfile /> if needed.
Interview question
What is the primary reason Vue Single-File Components (SFCs) necessitate a build tool for development?
- a.To automatically scope CSS styles and prevent them from leaking to other components.
- b.Browsers cannot directly parse or execute the custom .vue file format.Correct
- c.To allow developers to use modern JavaScript syntax and TypeScript within the script block.
- d.To convert the component's template into highly efficient JavaScript render functions.
Why? this is the answer
The card states, "A .vue file is not understood by browsers. It requires a build tool..." This means the custom .vue format is not natively executable, making a build step essential for browser interpretation. While converting templates to render functions (Option D) is a crucial task performed by the build tool, it's a consequence of needing to process the .vue file, not the fundamental reason the build tool is required.
Just read this? Test yourself on what you have been reading.
Read the original → vuejs.org
- #vue
- #frontend
- #components
- #build-tools
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on vue — each one lists the topics its interview covers.
See open roles