Vue Components
🧩 Vue Components
A Vue application is essentially a tree of components.
What are Vue Components?
A Vue component is a self-contained unit that includes:
-
Template (HTML)
-
Logic (JavaScript)
-
Style (CSS)
✔ Reusable
✔ Maintainable
✔ Scalable
Why Components are Important
Without components:
-
Code becomes messy
-
UI is hard to manage
-
Reuse is difficult
With components:
-
Clean structure
-
Easy maintenance
-
Team-friendly development
Types of Vue Components
1️⃣ Root Component
The top-level component (usually App.vue).
2️⃣ Local Components
Used inside specific components.
3️⃣ Global Components
Available everywhere (not recommended excessively).
Creating a Vue Component (SFC)
📄 src/components/Hello.vue
Using a Component
Props (Passing Data to Components)
Parent → Child
✔ Props are read-only
✔ Used to pass dynamic data
Emits (Child → Parent Communication)
Slots (Passing HTML Content)
✔ Makes components flexible
Component Lifecycle (Overview)
Every component goes through phases:
-
created
-
mounted
-
updated
-
unmounted
Used for:
-
API calls
-
DOM access
-
cleanup
Component Naming Rules
✔ Use PascalCase for components
✔ Use kebab-case in templates
Component Folder Structure (Best Practice)
Common Mistakes ❌
❌ Mutating props directly
❌ Large components doing too much
❌ Overusing global components
❌ Tight coupling between components
Best Practices ✅
✔ One responsibility per component
✔ Use props & emits properly
✔ Use slots for layout components
✔ Split large components
✔ Prefer Composition API for logic reuse
Summary Table
| Feature | Purpose |
|---|---|
| Component | UI building block |
| Props | Parent → Child data |
| Emits | Child → Parent events |
| Slots | Content injection |
| Scoped CSS | Style isolation |
🏁 Conclusion
Vue components are the core of Vue development.
Mastering components means you can build clean, reusable, and scalable applications.
