🧩 1. What Are They?
Vue offers two ways to write components:
| API Style | Introduced In | Description |
|---|---|---|
| Options API | Vue 2 | Organize code by options (data, methods, computed, etc.) |
| Composition API | Vue 3 | Organize code by logic using functions inside a setup() block |
Both can coexist in the same app — they just use different syntax and patterns.
⚙️ 2. Options API Example
<template>
<div>
<h2>{{ message }}</h2>
<button @click="increment">Count: {{ count }}</button>
</div>
</template>
<script>
export default {
data() {
return {
message: 'Hello from Options API!',
count: 0
}
},
methods: {
increment() {
this.count++
}
},
mounted() {
console.log('Component mounted!')
}
}
</script>
🧠 Key Idea:
You use options like:
data()for reactive statemethodsfor functionscomputedfor derived valuesmounted(),updated()for lifecycle hooks
Everything is attached to this.
⚡ 3. Composition API Example
<template>
<div>
<h2>{{ message }}</h2>
<button @click="increment">Count: {{ count }}</button>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
const message = ref('Hello from Composition API!')
const count = ref(0)
function increment() {
count.value++
}
onMounted(() => {
console.log('Component mounted!')
})
</script>
🧠 Key Idea:
You use functions instead of object options:
ref()andreactive()for state- regular JS functions for logic
onMounted()etc. for lifecycle hooks- No
this— everything is scoped variables inside<script setup>
🔍 4. Main Differences
| Feature | Options API | Composition API |
|---|---|---|
| Syntax | Object-based (data, methods) | Function-based (setup(), ref, computed) |
| Logic Organization | Grouped by option type | Grouped by feature / logic |
| Reusability | Mixins or extends | Composables (functions that return reactive data) |
| Learning Curve | Easier for beginners | More flexible, but more abstract |
| Performance | Similar | Similar (Composition slightly more tree-shakable) |
| Used in | Vue 2 & 3 | Vue 3+ (recommended for new projects) |
🧰 5. When to Use Which?
| Situation | Recommendation |
|---|---|
| Small or beginner project | ✅ Options API — simpler to read |
| Large, scalable app | ✅ Composition API — easier to organize complex logic |
| Writing reusable logic (hooks/composables) | 🧩 Composition API |
| Upgrading Vue 2 project | 🕐 Stick with Options API or mix both gradually |
🧠 6. Example Comparison
Imagine you have a component that:
- Tracks mouse position
- Fetches user data
In Options API, you’d mix everything inside one large component under different sections.
In Composition API, you can move logic into reusable composable functions like useMouse() or useUser():
// useMouse.js
import { ref, onMounted, onUnmounted } from 'vue'
export function useMouse() {
const x = ref(0)
const y = ref(0)
const update = e => { x.value = e.pageX; y.value = e.pageY }
onMounted(() => window.addEventListener('mousemove', update))
onUnmounted(() => window.removeEventListener('mousemove', update))
return { x, y }
}
Then in your component:
<script setup>
import { useMouse } from './useMouse'
const { x, y } = useMouse()
</script>
✅ Summary
| Aspect | Options API | Composition API |
|---|---|---|
| Organizes code by | Component sections | Feature logic |
Uses this | ✅ Yes | ❌ No |
| Reusability | Mixins | Composables |
| Vue Version | 2 & 3 | 3+ |
| Recommended for new apps | ⚠️ Optional | ✅ Yes |
