KoderSolutionKoderSolution LogoKoderSolution Logo
HomeArticlesTutorialsToolsRun CodeForumAI Lab
🖖

Vue

Topic Hub & Articles

Vue Intro

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Get Started

10 min

Vue Directives

10 min

Vue Events

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Forms

10 min

Vue Components

10 min

Vue Props

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Methods

10 min

Vue Computed Properties

10 min

Vue Watchers

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Class and Style Bindings

10 min

Vue Conditional Rendering

10 min

Vue List Rendering

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Template Syntax

10 min

Vue Teleport

10 min

Vue Suspense

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Transitions

10 min

Vue Mixins

10 min

Vue Custom Directives

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Plugins

10 min

Vue Slots

10 min

Vue Provide / Inject

10 min

Recap Quiz

Gated

Milestone Assessment

Vue Dynamic Components

10 min

Vue Async Components

10 min

Composition API Intro

10 min

Recap Quiz

Gated

Milestone Assessment

ref vs reactive

10 min

Lifecycle Hooks

10 min

Dependency Injection

10 min

Recap Quiz

Gated

Milestone Assessment

Computed and Watch

10 min

Template Refs

10 min

Composable Functions

10 min

Script Setup

10 min

Progress
0%

0 / 32 Lessons

KoderSolutionKoderSolution LogoKoderSolution Logo

The world’s most advanced technical ecosystem for modern software engineers. Learn, build, and grow with next-generation developer tools and resources.

Facebook PageX (Twitter) ProfileYouTube ChannelContact Support

Engineering Newsletter

Subscribe for weekly software engineering tutorials, architecture guides, and developer tool updates.

Platforms

  • Technical Articles
  • Interactive Tutorials
  • AI Coding Lab
  • Developer Forum
  • Developer Tools

Pages

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of Service
  • Refund Policy
  • Disclaimer
  • Advertisement

Popular Topics

  • PHP
  • Laravel
  • Python
  • React.Js
  • MySQL
© 2026 KoderSolutionAll Rights Reserved
Developed Bymaksudur.dev
VueVue Tutorial
Lesson

Vue Components

10 min reading
Free Course

Vue Components: Encapsulation and Single File Architecture

Vue components are reusable, self-contained building blocks that package markup, JavaScript logic, and CSS together. Instead of writing one giant HTML file, you split your interface into small components like headers, cards, and buttons.

In modern Vue 3 projects, each component lives in a .vue Single File Component (SFC). An SFC keeps related code together while isolating styles so one component never breaks another.

flowchart TD
    App["App.vue"] --> Header["AppHeader.vue"]
    App --> Main["DashboardView.vue"]
    Main --> Card1["MetricCard.vue"]
    Main --> Card2["MetricCard.vue"]

Anatomy of a .vue Single File Component

  • <script setup>: Runs when the component initializes. Any imported component or top-level variable is available directly in the template.
  • <template>: Holds the HTML structure and Vue directives. Vue 3 supports multiple root elements in a template.
  • <style scoped>: Adds unique attributes at compile time so CSS rules apply only to elements in this component.

Practical SFC Composition Example

First, create a reusable child component MetricCard.vue that receives title and value via defineProps().

<script setup>
defineProps({
  title: String,
  value: [String, Number]
});
</script>

<template>
  <div class="metric-card">
    <h3>{{ title }}</h3>
    <p class="value">{{ value }}</p>
  </div>
</template>

<style scoped>
.metric-card {
  padding: 1rem;
  border-radius: 6px;
  background-color: #ffffff;
  border: 1px solid #e2e8f0;
}
.value {
  font-size: 1.5rem;
  font-weight: bold;
  color: #2c3e50;
}
</style>

Next, import and render MetricCard inside App.vue. With <script setup>, importing the file automatically registers it for template use.

<script setup>
import MetricCard from './components/MetricCard.vue';
</script>

<template>
  <main class="dashboard">
    <h2>Dashboard Overview</h2>
    <div class="grid">
      <MetricCard title="Total Sales" value="$42,500" />
      <MetricCard title="Active Users" :value="1280" />
    </div>
  </main>
</template>

Common Mistakes

  • Single-Word Component Names: Naming a file Card.vue or Header.vue can collide with native or future HTML elements. Always use multi-word PascalCase names like MetricCard.vue or AppHeader.vue.
  • Forgetting scoped on Styles: Omitting scoped makes CSS rules global across the entire app, causing unexpected style leaks.
  • Mutating Component State Across Instances: Keep state inside <script setup> rather than module-level variables unless you intentionally want all instances to share one variable.

Frequently Asked Questions

Do Vue 3 components still need a single root <div>?

No. Vue 3 supports fragments, so a <template> can contain multiple sibling elements at the top level without an extra wrapper <div>.

Do I need components: { MetricCard } when using <script setup>?

No. Importing a .vue component inside <script setup> registers it in the template automatically.

When should I split code into a new component?

Split a section into a new component when you repeat that UI pattern across pages or when a single .vue file becomes hard to read and test.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Related Developer Tools & Topic Hub

Vue Topic Hub
JSON Formatter
Validate and format JSON payloads
HTML Formatter
Format HTML templates
CSS Formatter
Clean and format stylesheets
All Vue Articles
Deep-dive guides, tips & discussions
PreviousVue Forms
Continue JourneyNext: Vue Props

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum