Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Components  ›  Lesson

Single File Components

Single File Components 13 minA .vue file keeps one component's logic, template and styles together in script setup, template and style blocks.

Single File Components

Shelf will show a card for every book: title, page count and a button to mark it read. In plain web code that card would be spread across an HTML file, a JavaScript file and a CSS file, and you would hunt through all three to change it. Vue puts one component in one file.

The rule

One .vue file, three blocks: logic, markup, styles.

A single file component, or SFC, is a file ending in .vue. Here is Shelf's first card, src/components/BookCard.vue:

<script setup>
import { ref } from 'vue'

const title = 'Dune'
const read = ref(false)

function toggle() {
  read.value = !read.value
}
</script>

<template>
  <article class="card">
    <h3>{{ title }}</h3>
    <button @click="toggle">{{ read ? 'Finished' : 'Reading' }}</button>
  </article>
</template>

<style scoped>
.card { border: 1px solid #ccc; padding: 1rem; }
</style>
  • <script setup> holds the logic. Every variable, function and import at its top level is automatically usable in the template. No export or return is needed.
  • <template> holds the markup. {{ title }} prints a value; @click listens for a click. A ref such as read is written plainly here, while script code reads and writes read.value.
  • <style scoped> holds CSS. The word scoped makes Vue stamp this component's elements with a unique attribute, so .card here cannot restyle a .card in another component.

ref(false) makes a reactive value: when it changes, Vue re-renders the parts of the template that use it. You will study ref properly in the API Styles section; for now, know that changing read.value updates the button text.

The shortcut

Read any SFC by asking three questions in order: what does it know (script), what does it show (template), how does it look (style).

Reading BookCard.vue in three passesStep 1 of 4
  1. What does it know?

    In the script: a fixed title, a reactive read flag starting at false, and a toggle function that flips it.

A frequent slip is writing read = true in the script. read is the ref box itself, and the value inside lives in .value. The template unwraps it for you; the script does not.

Drills

Answer fast — trust the rule.

quick drill

In BookCard.vue, where does the toggle function live?

quick drill

What does scoped on the style block do?

quick drill

Inside <script setup>, how do you set the ref read to true?

quick drill

Which variables in <script setup> can the template use?

Remember

  • An SFC is one .vue file with <script setup>, <template> and <style> blocks.
  • Shortcut: read it as knows, shows, looks.
  • Trap: forgetting .value on a ref in script code; the template unwraps it, the script does not.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainThree blocks, three jobs

Describe the BookCard.vue file from this lesson block by block. For each block say what it holds and one rule about it, including what scoped changes.