Table of contents
1.
Introduction
2.
What is SFC?
3.
How Does SFC Work?
4.
Advantages of Using SFC
5.
Separation of Concerns
6.
FAQs
7.
Key Takeaways
Last Updated: Mar 27, 2024

Single File Components

Author Rajat Agrawal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

If you are learning Vue Js or have already worked on Vue Js, you must have heard about Single File Components (abbreviated as SFC).

Single File Components are the .vue files you create in a Vue project. There are various advantages of using SFC in a Vue project.

Let's discuss everything about Single File Components in depth.

What is SFC?

Single File Components (SFC) are the .vue files present in the Vue project.

.vue file is a file format that basically uses HTML-like syntax to describe a part of the User Interface (UI). 

Each *.vue file consists of three types of top-level language block.

<template>
</template>

<script>
</script>

<style>
</style>
You can also try this code with Online Javascript Compiler
Run Code

The <template> block is like the HTML of our UI.

The <script> block can maintain the logic and functionality of our app. 

The <style> block is where CSS is specified. All the styles related to the markup are done here. 

 

Let’s understand the above explanation with an example.

We can change App.vue file to the following:

<template>
<div id="app">
  <h1>{{fname}} {{lname}}</h1>
  <h2>{{greet}}</h2>
</div>
</template>

<script>
export default {
  name: 'App',
  data(){
      const fname = 'Coding'
      const lname = 'Ninjas'
      const greet = `Hello from ${fname} ${lname}!`;
    return{ 
      fname,
      lname,
      greet
    }
  }
}
</script>

<style>
#app{
  font-family: Avenir, Helvetica, Arial, sans-serif;
  text-align: center;
}
 h1{
  color:blue;
}
h2{
  color:red;
}
</style>
You can also try this code with Online Javascript Compiler
Run Code

Output:

We can clearly see how all three blocks are bound to create a UI. We bind the template and script with the help of mustache syntax ( {{ }} ). We set the color for h1 as blue and the color for h2 as red, which is successfully reflected in the UI. 

How Does SFC Work?

The browser does not understand what a .vue file is, so in the actual project, we generally  integrate the SFC compiler with a build tool such as Vite or Vue CLI(which is based on webpack). 

The webpack with the vue loader is going to parse the file, extract each of the three blocks(<script>,<template> and <style>) and pipe them through other loaders if necessary and finally assemble them back into a format that the browser can understand.

Since we will use build tools like Vite or Vue CLI, we don’t need to worry about the whole build process. 

A compiled SFC is a standard JavaScript (ES) module - which means with proper build setup, you can import an SFC like a module:

<script>
import MyComponent from './MyComponent.vue'

export default {
  components: {
    MyComponent
  }
}
</script>
You can also try this code with Online Javascript Compiler
Run Code

Advantages of Using SFC

1.) We need Single File Components because the components defined using Vue.component can’t scale into larger projects.

2.) The codebase is divided into one or more .vue files where each file is responsible for its own markup, styles, and logic. It helps in scalability.

3.) We can create modularized components using familiar HTML, CSS, and JavaScript syntax.

4.) We get syntax highlighting, modules, and component-scoped CSS with single-file components.

5.) SFC provides more compile-time optimizations by cross-analyzing template and script.

Separation of Concerns

In vue, instead of dividing the codebase into three huge layers that form the User Interface, the codebase is instead divided into one or more .vue files where each file is responsible for its markup, styles, and logic.

It is being said that SFCs are mixing different concerns in the same place - which HTML/CSS/JS were supposed to separate. 

The best answer to this problem is that the separation of concerns is not equal to the separation of file types. Our utmost goal must be to improve the maintainability of codebases. Separation of concerns, when applied directly as separation of file types, does not help us reach that goal in the context of huge front-end applications.

In modern front-end development, we have found that instead of dividing the codebase into three huge layers that interlinked with one another, it makes much more sense to divide them into loosely-coupled components and compose them. Inside a component, its template, logic, and styles are inherently bound, and grouping them actually makes the component more cohesive and maintainable.

FAQs

  1. What are the top-level blocks present in SFC?
    SFC consists of three types of top-level blocks <template>, <script> and <style>.
     
  2. Is SFC better than Vue.component?
    Yes, SFC is better than the Vue.component in terms of the project's scalability.
    If we want to use the Vue.component on a larger project, we have to create an element to house them and our templates to string in a script tag. Strings don't have syntax highlighting support for templates. They are both a pain if a component is more extensive or has lots of nesting. 
     
  3. What are the common build tools used with the SFC compiler?
    Vite and Vue CLI are the most common build tools used with the SFC compiler to parse the .vue file.

Key Takeaways

This article taught about Single File Components in Vue Js and the benefits of using SFC in our Vue project.

If you want to read more exciting web development blogs, you can visit Coding Ninjas Web BlogsAlso, If you are curious to learn advanced front-end web development, Coding Ninjas is here with one of the best courses available, which you can find here

Happy Learning!!

Live masterclass