Vue.js course Β· Module 6: Components
Components - Base Building Modules
In this lesson5
Welcome to the NOVA LAB 3D Printer Bay! Dr. Nova says: just as the 3D printer creates modular parts of the Martian habitat, you'll learn to create Vue components - reusable application elements.
Until now, the whole panel lived in a single App.vue file. That's enough for one screen, but the base keeps growing: five identical module cards mean five copies of the same HTML, and a fix in one card means fixing all five. A component solves this problem - you design an element once and use it as many times as you like.
What you will learn
- how to create components and name them by convention,
- how to import them locally and register them globally,
- how to build an interface from a tree of components,
- how to run code in the successive phases of a component's life,
- how to isolate styles and extract shared logic into composables.
What is a Component?
A component is a reusable, self-contained UI unit with its own template, logic, and styles - like a habitat module that can be connected with others. Here is a module card saved in the ModuleCard.vue file, starting with the template and the data:
<!-- ModuleCard.vue -->
<template>
<article class="module-card" style="background: #0a0e27; border: 1px solid #00b4d8; color: #00ff88;">
<div class="module-icon">β</div>
<h3>{{ moduleName }}</h3>
<p>{{ moduleType }}</p>
<p class="status">{{ status }}</p>
</article>
</template>
<script setup>
import { ref } from 'vue'
const moduleName = ref('Oxygen Generator Alpha')
const moduleType = ref('Life Support')
const status = ref('ACTIVE')
</script>The template shows the module's name, type and status, and three ref() containers hold this data - the same syntax you used in the Launch Zone. The only new thing is that the file has its own name and will be used somewhere else. We write the card's housing in the same file, in a section with styles limited to the component:
<style scoped>
.module-card {
border-radius: 8px;
padding: 1rem;
max-width: 300px;
}
.module-icon {
font-size: 3rem;
text-align: center;
}
.status {
color: #00ff88;
font-weight: bold;
}
</style>We arrange the sections in the order used at NOVA LAB since the Launch Zone: <template>, <script setup>, <style scoped>. In the editor you'll also see files with <script setup> at the top - that's correct too, because Vue only requires consistency within a project and the style last.
Using a Component
To print the card in the base, we import the file in App.vue and use the import name like an HTML tag:
<!-- App.vue -->
<template>
<div class="production-bay" style="background: #0a0e27;">
<ModuleCard />
<ModuleCard />
<ModuleCard />
</div>
</template>
<script setup>
import ModuleCard from './components/ModuleCard.vue'
</script>Every <ModuleCard /> tag creates a separate instance of the component with its own data, so changing the status in one card won't touch the others. The ModuleCard.vue file hasn't changed and won't change no matter how many copies you place. For now all three cards show the same data - passing different values to each card, known as props, is something you'll learn at the Communication Station. This is the main advantage of a component architecture: you write the code once, reuse it many times, and make a fix in one place.
Component Naming
We write component names in PascalCase, meaning every word starts with a capital letter, with no spaces or underscores. We name the files the same way: ModuleCard.vue, not module_card.vue or moduleCard.vue:
<script setup>
// PascalCase - recommended
import ArtworkCard from './ArtworkCard.vue'
import UserProfile from './UserProfile.vue'
import NavigationMenu from './NavigationMenu.vue'
// In template - PascalCase or kebab-case
</script>
<template>
<!-- PascalCase -->
<ArtworkCard />
<UserProfile />
<!-- or kebab-case -->
<artwork-card />
<user-profile />
</template>Both tag forms work in .vue file templates, but I recommend PascalCase - you can see at a glance that <UserProfile /> is a component and <div> is plain HTML. A second rule from the official style guide says a component name should have at least two words. All HTML elements have single-word names, so a Card component could clash with a future browser tag, while ModuleCard never will. The exception is the root App component, and base components get a prefix, for example BaseButton.
File Organization
As the number of components grows, we group them in subfolders of src/components/ according to the role they play in the base:
src/
βββ components/
β βββ common/
β β βββ BaseButton.vue
β β βββ BasePanelCard.vue
β β βββ BaseInput.vue
β βββ modules/
β β βββ ModuleCard.vue
β β βββ ModuleList.vue
β β βββ ModuleDetail.vue
β βββ layout/
β βββ MissionHeader.vue
β βββ ControlFooter.vue
β βββ NavSidebar.vueThe common/ folder holds base components, modules/ the module cards and lists, and layout/ the elements of the screen frame. Reusable components always go to src/components/, while whole screens connected to navigation go to the src/views/ folder you know from the project structure.
In the editor below, App.vue uses the PrinterStatusPanel component three times. You'll see defineProps and defineEmits in it - these are the communication channels of the Communication Station, and for today it's enough that the panel is a separate file, imported and used like a tag.
Remember: a component is a printed habitat module - you design it once, in a file with a multi-word PascalCase name, and mount it in the base as many times as you need.
Code for this lesson: App.vue
<script setup>
import { ref } from 'vue'
import PrinterStatusPanel from './PrinterStatusPanel.vue'
const printers = ref([
{
id: 'p1',
name: '3D Printer Alpha',
status: 'printing',
progress: 67,
material: 'PLA Composite',
temperature: 210
},
{
id: 'p2',
name: '3D Printer Beta',
status: 'idle',
progress: 0,
material: 'Carbon Fiber',
temperature: 25
},
{
id: 'p3',
name: '3D Printer Gamma',
status: 'error',
progress: 45,
material: 'Titanium Alloy',
temperature: 185
}
])
function handlePrinterAction(printerId, action) {
const printer = printers.value.find(p => p.id === printerId)
if (printer) {
console.log(`Action ${action} on ${printer.name}`)
if (action === 'start') {
printer.status = 'printing'
} else if (action === 'stop') {
printer.status = 'idle'
printer.progress = 0
} else if (action === 'pause') {
printer.status = 'paused'
}
}
}
</script>
<template>
<div class="printing-lab">
<div class="header">
<h1>3D Print Lab - NOVA LAB</h1>
<p class="subtitle">3D printer control panel on Mars</p>
</div>
<div class="info-panel">
<h3>Defining components with props and emits</h3>
<p>Components receive data through <strong>props</strong> and communicate through <strong>emits</strong></p>
</div>
<div class="printers-grid">
<PrinterStatusPanel
v-for="printer in printers"
:key="printer.id"
:printer-id="printer.id"
:printer-name="printer.name"
:status="printer.status"
:progress="printer.progress"
:material="printer.material"
:temperature="printer.temperature"
@action="handlePrinterAction"
/>
</div>
</div>
</template>
<style scoped>
.printing-lab {
background: linear-gradient(135deg, #0a0e27 0%, #1a1e3f 100%);
min-height: 100vh;
padding: 2rem;
color: #ffffff;
font-family: 'Courier New', monospace;
}
.header {
text-align: center;
margin-bottom: 2rem;
padding-bottom: 1rem;
border-bottom: 2px solid #00ff88;
}
.header h1 {
font-size: 2.5rem;
color: #00ff88;
text-shadow: 0 0 20px rgba(0, 255, 136, 0.5);
margin: 0;
}
.subtitle {
color: #00b4d8;
font-size: 1.2rem;
margin-top: 0.5rem;
}
.info-panel {
background: rgba(0, 180, 216, 0.1);
border: 2px solid #00b4d8;
border-radius: 10px;
padding: 1.5rem;
margin-bottom: 2rem;
}
.info-panel h3 {
color: #00b4d8;
margin-top: 0;
}
.info-panel strong {
color: #00ff88;
}
.printers-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
gap: 1.5rem;
}
</style>Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is a component in Vue.js?
2. What is the main advantage of component-based architecture in Vue?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Code editor
Create a MarsModule.vue component with template, script setup, and style scoped sections.
- Click in order
Arrange the sections of a Single File Component in the order used at NOVA LAB:
- Code editor
Create a StatusPanel.vue component with ref() for status and temperature displayed in the template.