Table of Contents
Key Parts of an SFC
Why Use SFCs?
Home Web Front-end Vue.js What is a single file component (SFC)?

What is a single file component (SFC)?

Jul 25, 2025 am 03:40 AM

A Single File Component (SFC) in Vue.js combines template, script, and style into one .vue file for better organization. 1. The

What is a single file component (SFC)?

A Single File Component (SFC) is a file format used primarily in Vue.js that encapsulates the template, script, and style of a component into one .vue file.

What is a single file component (SFC)?

Instead of splitting a component’s HTML (template), JavaScript (logic), and CSS (styles) across multiple files, an SFC lets you write them together in a single, self-contained unit. This makes components easier to organize, maintain, and reuse.

Here’s what a typical Vue SFC looks like:

What is a single file component (SFC)?
<template>
  <div class="greeting">
    <h1>{{ message }}</h1>
  </div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello from SFC!'
    }
  }
}
</script>

<style scoped>
.greeting {
  color: blue;
  text-align: center;
}
</style>

Key Parts of an SFC

  • <template></template> – Defines the HTML structure of the component.
  • <script></script> – Contains the JavaScript logic (e.g., component options, data, methods).
  • <style></style> – Holds CSS that styles the component. Using scoped ensures styles only apply to this component.

Why Use SFCs?

  • Better organization: All related code lives in one file.
  • Component scoping: Styles can be scoped to avoid global CSS conflicts.
  • Tooling support: Build tools like Vite or Vue CLI can process SFCs with features like hot module replacement.
  • Reusability: Components are modular and easy to import/export.

SFCs are a Vue-specific convention and require a build step (like with Vite or Webpack) to parse and compile them into standard JavaScript and CSS that browsers can understand.

Basically, SFCs make Vue development cleaner and more intuitive by keeping everything about a component in one place.

What is a single file component (SFC)?

The above is the detailed content of What is a single file component (SFC)?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1504
276
How to build a component library with Vue? How to build a component library with Vue? Jul 10, 2025 pm 12:14 PM

Building a Vue component library requires designing the structure around the business scenario and following the complete process of development, testing and release. 1. The structural design should be classified according to functional modules, including basic components, layout components and business components; 2. Use SCSS or CSS variables to unify the theme and style; 3. Unify the naming specifications and introduce ESLint and Prettier to ensure the consistent code style; 4. Display the usage of components on the supporting document site; 5. Use Vite and other tools to package as NPM packages and configure rollupOptions; 6. Follow the semver specification to manage versions and changelogs when publishing.

Free entrance to Vue finished product resources website. Complete Vue finished product is permanently viewed online Free entrance to Vue finished product resources website. Complete Vue finished product is permanently viewed online Jul 23, 2025 pm 12:39 PM

This article has selected a series of top-level finished product resource websites for Vue developers and learners. Through these platforms, you can browse, learn, and even reuse massive high-quality Vue complete projects online for free, thereby quickly improving your development skills and project practice capabilities.

What is the purpose of v-bind directive? What is the purpose of v-bind directive? Jul 10, 2025 pm 12:47 PM

v-bind is used in Vue.js to dynamically bind one or more attributes or components to expressions. It enables dynamic updates by keeping DOM properties synchronized with Vue instance data. Common usages include binding src, href, class and style attributes, such as using to implement dynamic updates of image sources; dynamically switch classes through: class="{active:isActive}"; using: style="{color:textColor}" to set inline style; and can also pass objects to bind multiple attributes at the same time, such as v-bind="{id:myId}"; when value is required

What are Vue lifecycle hooks? Name a few and explain their use cases. What are Vue lifecycle hooks? Name a few and explain their use cases. Jul 24, 2025 am 12:08 AM

The life cycle hook of the Vue component is used to execute code at a specific stage. 1.created: Called immediately after the component is created, suitable for initializing data; 2.mounted: Called after the component is mounted to the DOM, suitable for operating the DOM or loading external resources; 3.updated: Called when the data update causes the component to be re-rendered, suitable for responding to data changes; 4.beforeUnmount: Called before the component is uninstalled, suitable for cleaning event listening or timer to prevent memory leakage. These hooks help developers accurately control component behavior and optimize performance.

How to access store instance in Composition API? How to access store instance in Composition API? Jul 10, 2025 pm 12:49 PM

The main way to access the store in the Vue3Composition API is to useStore() or directly call the Pinia defined store. 1. When using Vuex4, you need to import the useStore and call it in setup(). After obtaining the store instance, you can access state, dispatch, etc.; 2. When using Pinia, you can import and call it directly after defining the store, without usingStore; 3. Access the store outside the setup(). Vuex needs to export the store instance and reference it, and Pinia can call useXxxStore() anywhere; 4. Common questions include checking sto

What is Teleport in Vue 3? What is Teleport in Vue 3? Jul 10, 2025 pm 12:50 PM

TeleportinVue3allowsrenderingacomponent'sHTMLinadifferentpartoftheDOMwhilemaintainingnormaldataflow.1.Itsolvesissueslikemodals,tooltips,anddropdownsbeingclippedorhavingz-indexproblemsbymovingthemtoaspecifiedlocationsuchasbodyoraspecificelementlike#mo

How to create and use a Vue plugin? How to create and use a Vue plugin? Jul 10, 2025 pm 12:38 PM

How to create and use Vue plugin? The Vue plugin is an object with the install method to add global functionality to the entire application. 1. The basic structure of the plug-in is to define an object containing the install method, which receives app and options parameters; 2. In the install method, you can add global methods, register components or instructions, inject mixed in, etc.; 3. Use app.use() to register the plug-in and pass in options; 4. Get plug-in methods through this.$method or Composition API in the component; 5. It is recommended to keep a single responsibility, support configuration parameters, avoid naming conflicts, and improve compatibility with Composition API; 6

How to install vue router? How to install vue router? Jul 12, 2025 am 02:11 AM

The steps to install VueRouter are as follows: 1. Confirm that the project is created based on Vue3 and check the vue version in package.json; 2. Run npminstallvue-router@4 or yarnaddvue-router@4 in the terminal to install dependencies; 3. Create router.js file and configure the routing table, and initialize the routing instance using createRouter and createWebHistory; 4. Introduce and mount the routing instance in main.js to the application; 5. Note that the page needs to be included and used for navigation, and configure the server to support history mode during deployment.

See all articles