Chapter 27: Local Components

What are “Local Components”?

In Vue, local components (also called locally registered components) are components that are only available inside one specific parent component.

They are not globally registered — which means:

  • You cannot use them anywhere else in your app unless you import and register them again
  • They are private to the file where they are declared

This is the opposite of global components (which you register once in main.ts with app.component(‘MyButton’, MyButton) and can use everywhere).

In modern Vue 3 (especially with <script setup>), almost all components are local by default — and that’s actually a good thing for most projects.

Why Local Components Are the 2026 Default (Big Advantages)

  1. No naming pollution You can have Button.vue in many different folders with completely different styles/logic — no conflict
  2. Better tree-shaking & bundle size Vite only includes components you actually import
  3. Clearer mental model When you see <MySpecialCard /> — you know exactly where to find its code (just look at imports)
  4. Easier refactoring Move/rename a component → only change one import path
  5. Encapsulation & predictability No surprise “why is this component working here but not there?”

How to Use Local Components (Modern <script setup> Way)

You just import the component and use it in the template — that’s it. No components: {} object needed anymore.

Real Example – Dashboard with Local Sub-Components

Imagine you’re building a user dashboard page.

File: src/views/DashboardView.vue (the page)

vue

Child 1: StatsCard.vue (local helper – only makes sense in dashboard context)

vue

Child 2: RecentActivity.vue (also local)

vue

Folder Structure Recommendation (2026 Pattern)

text

→ This is very common in medium/large apps → Local components live next to the page that needs them

Global vs Local – Quick Decision Table

Situation Use Global Registration? Use Local (import)? Why?
Button, Card, Modal, FormInput Yes (in main.ts or plugin) No Used on 20+ pages
StatsCard, RecentActivity (dashboard-specific) No Yes Only used in one place
Page header, sidebar, footer Yes No Repeated across app
One-off complex widget (only on /profile) No Yes No need to pollute global scope

Summary – Local Components in 2026

  • Default & recommended in modern Vue 3 projects
  • Just import → use in template
  • No components: {} registration needed
  • Keep them close to the page/feature that uses them (components/dashboard/, components/profile/, etc.)
  • Makes code easier to find, refactor, and reason about
  • Scales beautifully — no global namespace pollution

Your mini homework:

  1. Create a ProfileHeader.vue local component inside views/ProfileView.vue
  2. Move StatsCard from global to local (only used in Dashboard)
  3. Check bundle size before/after (spoiler: local = better tree-shaking)

Any part confusing? Want to see:

  • How to make a component global later if needed?
  • Local vs global performance impact?
  • Folder-by-feature vs folder-by-type debate?
  • Real example with nested local components?

Just tell me — we’ll build the next piece together 🚀

Happy local-component-ing from Hyderabad! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *