Chapter 21: Vue Components

Vue Components, the single most important building block in Vue. Once you really understand components, everything else in Vue (routing, state, forms, styling) suddenly makes a lot more sense.

Think of a Vue component as a reusable, self-contained Lego piece of your user interface:

  • It has its own HTML structure (template)
  • It has its own JavaScript logic (state, methods, computed…)
  • It has its own CSS styles (scoped so they don’t leak)
  • It can receive data from parents (props)
  • It can send events up to parents (emits)
  • It can be used anywhere — like <MyButton />, <TodoItem />, <UserCard />

In Vue 3 (2026 standard), almost everything you see on screen is a component — even <div>, <button>, <input> are technically native HTML elements, but we build our UI by composing our own custom components.

Two Main Flavors in Vue 3 (you need to know both)

  1. Options API (classic style — still very common in old projects & tutorials)
vue
  1. Composition API + <script setup> (modern, recommended since ~2022 — 90%+ of new code in 2026)
vue

From today onward — we will use <script setup> (the future-proof way).

Real Example: A Classic First Component → TodoItem.vue

Let’s build a realistic, reusable todo item component.

File: src/components/TodoItem.vue

vue

How to Use This Component (Parent → Child)

File: src/views/TodoList.vue (a page that uses many TodoItem components)

vue

Quick Summary Table – Core Component Concepts

Concept What it is Syntax (script setup) Example from above
Template HTML-like UI structure <template>…</template> <div class=”todo-item”>…</div>
Props Data passed from parent → child defineProps<{ todo: Todo }>() :todo=”todo”
Emits Events sent from child → parent defineEmits<{ (e: ‘toggle’, id: number): void }>() @toggle=”toggleTodo”
State Internal reactive data const count = ref(0) (not in child, but in parent)
Scoped Styles CSS only for this component <style scoped>…</style> .todo-item { … }
Composition API Modern way to write logic <script setup> Whole <script setup> block

Best Practices You Just Learned (2026 style)

  • Always use :key in v-for (we used :key=”todo.id”)
  • Use TypeScript interfaces for props (makes large apps much safer)
  • Prefer defineProps / defineEmits without extra props: / emits: arrays
  • Keep child components dumb & reusable — they receive props & emit events
  • Parent owns the state → child just displays & notifies

Your Next Mini Homework

  1. Create TodoItem.vue exactly as above
  2. Create TodoList.vue (or put it in HomeView.vue)
  3. Add one more feature: a button in TodoItem that emits ‘edit’
  4. In parent → when ‘edit’ → prompt for new text and update

Any part still fuzzy? Want me to show:

  • How to make TodoItem editable inline?
  • Global component registration vs local imports?
  • Props validation / default values?
  • Slots (for customizable content inside component)?

Just say — we’ll build the next piece together 🚀

Happy component composing from Hyderabad! 💙

You may also like...

Leave a Reply

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