0
0
Vueframework~5 mins

Why components are essential in Vue

Choose your learning style9 modes available
Introduction

Components help break a big app into small, easy parts. Each part does one job, making the app simple to build and fix.

When building a website with repeating parts like buttons or cards.
When you want to reuse the same design or code in many places.
When working with a team so each person can work on different parts.
When you want to keep your code clean and easy to understand.
When you want to update one part without changing the whole app.
Syntax
Vue
<template>
  <MyComponent />
</template>

<script setup>
import MyComponent from './MyComponent.vue'
</script>
Use <template> to define the HTML structure of a component.
Use <script setup> for the component logic in Vue 3.
Examples
A simple button component showing basic structure.
Vue
<template>
  <button>Click me</button>
</template>
A component showing how to use data inside the template.
Vue
<template>
  <p>{{ message }}</p>
</template>

<script setup>
const message = 'Hello from component!'
</script>
Using one component inside another to build bigger parts.
Vue
<template>
  <ChildComponent />
</template>

<script setup>
import ChildComponent from './ChildComponent.vue'
</script>
Sample Program

This example shows a button component that counts clicks. The main app uses this button component. This keeps code simple and reusable.

Vue
<!-- ButtonComponent.vue -->
<template>
  <button @click="count++">Clicked {{ count }} times</button>
</template>

<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>


<!-- App.vue -->
<template>
  <h1>My App</h1>
  <ButtonComponent />
</template>

<script setup>
import ButtonComponent from './ButtonComponent.vue'
</script>
OutputSuccess
Important Notes

Components make your app easier to manage by splitting it into small pieces.

Each component can have its own data and behavior.

Reusing components saves time and keeps design consistent.

Summary

Components break big apps into small, manageable parts.

They help reuse code and design easily.

Using components makes teamwork and updates simpler.