Your First Component — Vue

A component is one piece of the interface with its own HTML, logic (data and functions) and CSS. In a real project, a component lives in a .vue file (a Single…

The page for Mrs. Sri's stall is getting long: the sign at the top, the menu in the middle, the copyright line at the bottom. If it's all written in one template, changing the sign means digging through a file that also holds the menu and the copyright line.

In Vue, you split that page into components: self-contained pieces of the interface, each with its own HTML, logic (data and functions) and CSS. Think of the stall's display shelf built from separate boxes. Each box is assembled once and can be placed anywhere. If the sign box is replaced, the other boxes stay in place. The shelf is the page, and each box is one component. One component usually lives in one file ending in .vue.

Anatomy of a .vue file

A .vue file is called a Single File Component (SFC): one file with three blocks. Here is an example, the footer (the bottom part of the page) of the stall:

<!-- StallFooter.vue -->
<script setup>
import { ref } from "vue"

const year = ref(2026)
</script>

<template>
  <footer>
    <p>© {{ year }}</p>
  </footer>
</template>

<style scoped>
p {
  color: #b45309;
  font-weight: 700;
}
</style>

A .vue file and the exercise editor

The BelajarWeb.dev exercise editor runs one component, written without the block tags (<script setup>, <template>, <style>):

In a .vue file In the exercise editor
the inside of <script setup>, without the import line the part above the ---template--- line
the inside of <template> the part below the ---template--- line
<style> has no place

When the component needs no data, the code in the editor starts right at the ---template--- line. Here is one stall page in the editor:

---template---
<div>
  <header><h1>Mrs. Sri's Stall</h1></header>
  <main><p>Today's menu: Fried Rice, Chicken Noodles, Iced Tea</p></main>
  <footer><p>© 2026</p></footer>
</div>

Putting components together

In a real project, a page is assembled from several components. Here is App.vue, which uses two other components:

<!-- App.vue -->
<script setup>
import StallHeader from "./StallHeader.vue"
import StallFooter from "./StallFooter.vue"
</script>

<template>
  <StallHeader />
  <main>
    <p>Today's menu: Fried Rice, Chicken Noodles, Iced Tea</p>
  </main>
  <StallFooter />
</template>

One root or many

The root element is the outermost element of a template. The App.vue template above has three root elements: <StallHeader />, <main> and <StallFooter />.

Styles that don't leak: scoped

Without scoped, the rule p { color: #b45309 } in StallFooter.vue applies to every <p> on the page, including the menu paragraph that belongs to App.vue. With scoped, Vue adds a unique attribute to each element of that component, then rewrites the selector so it only matches elements with that attribute. The result:

<footer data-v-1f42fb90><p data-v-1f42fb90>© 2026</p></footer>
<style>
p[data-v-1f42fb90] { color: #b45309; font-weight: 700; }
</style>

The selector p[data-v-1f42fb90] only matches StallFooter's <p>, so the menu paragraph keeps its colour.

⚠️ What often goes wrong in a first component

TL;DR: A component is one piece of the interface with its own HTML, logic (data and functions) and CSS. In a real project, a component lives in a .vue file (a Single File Component) with three blocks: <script setup> for the logic, <template> for the HTML, and <style scoped> for the CSS. An imported component can be used as a tag, such as <StallHeader />. In the exercise editor, one component is written as the inside of the script, the ---template--- line, then the inside of the template, with no import and no <style>. In Vue 3, a template may have many root elements (outermost elements). Vue 2 needs exactly one. scoped makes CSS apply only to its own component.

⚡ One file, three blocks

Write:

<!-- StallFooter.vue -->
<script setup>
import { ref } from "vue"

const year = ref(2026)
</script>

<template>
  <footer>
    <p>© {{ year }}</p>
  </footer>
</template>

<style scoped>
p {
  color: #b45309;
  font-weight: 700;
}
</style>

Meaning: In a real project, one component (one piece of the interface) lives in one .vue file, called a Single File Component. This file has three blocks: <script setup> for the logic (data and functions), <template> for its HTML, and <style scoped> for CSS that only applies to this component.

⚡ In the exercise editor, no block tags

Write:

---template---
<div>
  <header><h1>Mrs. Sri's Stall</h1></header>
  <main><p>Today's menu: Fried Rice, Chicken Noodles, Iced Tea</p></main>
  <footer><p>© 2026</p></footer>
</div>
Guess first: the exercise editor separates the script and the template with the ---template--- line. If you paste a whole .vue file, with its <script setup> and <template> tags, into the exercise editor, what shows?

Nothing. Without the ---template--- line, everything is read as a template. The <script> is dropped, and the inside of <template> goes into an element that can't be seen. So the page is empty.

Meaning: In the exercise editor, the inside of the script goes above the ---template--- line, and the inside of the template goes below it. This component needs no data, so the code starts right at that line. In the exercises, the checker uses selectors such as header h1, which means an <h1> inside a <header>.

⚡ One root or many

Guess first: a template holds an <h1> and a <p>, with no one element around both. Is that an error in Vue 3?

No. Vue 3 allows a template with many outermost elements (it calls this a fragment). Vue 2 refuses it, because Vue 2 requires exactly one outermost element.

Meaning: The outermost element of a template is called the root element. In that example, the <h1> and the <p> are both root elements. A wrapping <div> is still common in Vue 3 because it's tidy.

What you will learn