Single File Components (SFCs) in Vue.js allow you to encapsulate the structure, behavior, and style of a component into one file. These files have the .vue
extension and consist of three primary sections:
<template>
: Defines the HTML structure of the component.<script>
: Contains the JavaScript code for the component, including the logic and behavior.<style>
: Defines the CSS styles scoped to the component.This separation within a single file provides a clean, organized way to structure your Vue components.
The basic structure of an SFC looks like this:
<template>
<div>
<h1>{{ message }}</h1>
<button @click="changeMessage">Change Message</button>
</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello, Vue.js!'
};
},
methods: {
changeMessage() {
this.message = 'Message Changed!';
}
}
};
</script>
<style scoped>
h1 {
color: #42b983;
}
button {
padding: 10px 20px;
background-color: #42b983;
color: white;
border: none;
}
</style>
<template>
: This section holds the HTML structure and bindings of the component. It can include dynamic expressions, like {{ message }}
, to display data from the component’s state.
<script>
: Contains the JavaScript logic for the component. It defines the component’s behavior, including data, methods, computed properties, and lifecycle hooks.
<style>
: This section contains CSS styles specific to the component. The scoped
attribute ensures that these styles are only applied to this component, preventing any global style conflicts.
Vue's SFCs provide numerous advantages over traditional methods of structuring components. Here are some key benefits:
By placing the HTML, JavaScript, and CSS in the same file, SFCs offer a clean separation of concerns. Each section (template, script, style) is clearly defined, making the component more organized and easier to manage.
SFCs encapsulate all the logic, structure, and styling within the component, reducing the risk of side effects between components. The scoped
styles feature ensures that styles defined in one component don’t leak into others.
When all the code for a component is in one place, it becomes much easier to read and understand, especially for developers who are new to the project. You don’t have to navigate between different files for HTML, JavaScript, and CSS.
SFCs allow for better tooling support, such as syntax highlighting, auto-completion, and linting, which makes the development process smoother and more efficient.
Here’s a simple example of a counter component that displays a message and allows the user to increment a counter value. The entire component is contained within a single .vue
file.
<template>
<div>
<h2>Counter: {{ counter }}</h2>
<button @click="increment">Increment</button>
</div>
</template>
<script>
export default {
data() {
return {
counter: 0
};
},
methods: {
increment() {
this.counter++;
}
}
};
</script>
<style scoped>
h2 {
color: #42b983;
}
button {
padding: 10px 20px;
background-color: #42b983;
color: white;
border: none;
cursor: pointer;
}
button:hover {
background-color: #367c60;
}
</style>
counter
variable in the data
section and an increment
method to increase the counter.To use SFCs in Vue, you’ll need to set up a Vue project, typically using Vue CLI. Here's how you can get started:
First, you need to install Vue CLI globally on your machine if you don’t have it yet.
npm install -g @vue/cli
Create a new Vue project using Vue CLI:
vue create my-vue-app
Choose the default preset or configure your project based on your requirements.
Once your project is set up, you can start creating SFCs by adding .vue
files in the src/components/
directory.
For example, create a new file called Counter.vue
and use the structure mentioned earlier.
App.vue
You can now import and use your component in the main App.vue
file.
<template>
<div id="app">
<Counter />
</div>
</template>
<script>
import Counter from './components/Counter.vue';
export default {
components: {
Counter
}
};
</script>