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>
<script setup>holds the component's logic. Variables and functions made here, and components imported here, can be used in the template right away. Unlike in the exercise editor, theimportline is required in a.vuefile.<template>holds the markup (its HTML), with Vue syntax such as{{ year }}.<style scoped>holds CSS that only applies to this component's elements.- File names use PascalCase (each word starts with a capital letter, no spaces) and have more than one word, such as
StallFooter.vue. That way they don't clash with HTML tags like<footer>.
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>
<header>,<nav>,<main>and<footer>are semantic elements: elements whose names say the role of their part. They look the same as a<div>. The difference is that the browser and screen readers (programs that read pages aloud) then know which part is the head of the page (<header>), the navigation (<nav>), the main content (<main>) and the foot of the page (<footer>).- In the exercises, the checker uses CSS selectors such as
header h1. That selector means an<h1>inside a<header>. So the<h1>has to go inside the<header>, not next to it. - Don't paste a whole
.vuefile into the editor. 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. The result is an empty page.
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>
- A component imported in
<script setup>can be used as a tag right away:<StallHeader />. The tag may close itself with/>, with no separate closing tag. - A component that uses other components is called the parent, here
App.vue. The components it uses are called children:StallHeaderandStallFooter. - If you forget the
importline, Vue doesn't recogniseStallHeader. In development mode, the console writes[Vue warn]: Failed to resolve component: StallHeader. On the page, only an empty<stallheader>tag appears. - If you place
<StallHeader />twice, you get two copies. How to send different data to each copy (props) comes in a later lesson.
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 />.
- In Vue 3, a template may have many root elements. A component like this is called a fragment. For example, a template with an
<h1>and a<p>and no wrapper (one element around both) works normally. - In Vue 2, a template must have exactly one root element. The
App.vuetemplate above is refused with the errorComponent template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead.The fix: wrap everything in one<div>. - A wrapping
<div>is still common in Vue 3 because it's tidy, and the starter code in the exercise editor uses one too.
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
- Pasting a whole .vue file into the exercise editor. The page is empty. In the editor, write the inside of the script, the
---template---line, then the inside of the template. - An element in the wrong place. In the exercises, the checker looks for
header h1,footer pandmain p. An<h1>outside the<header>doesn't count, even if it looks similar. - Text that isn't exact. In the exercise,
© 2026needs the © symbol, a space, then the year. You can type the symbol or write©. <style>withoutscoped. Its rules leak to the whole page and colour other components' elements.
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.