What is the difference between the Options API and the Composition API in Vue?
Understand the difference between Vue's Options API and Composition API, when to use each, and how composables enable cleaner, reusable component logic.
Expected Interview Answer
The Options API organizes a component by fixed option buckets (data, methods, computed, watch), while the Composition API organizes logic by feature using setup() and standalone reactive functions like ref, reactive and computed.
In the Options API, a single concern is scattered across separate options, so related code drifts apart as a component grows. The Composition API lets you group everything about one feature in one place and extract it into reusable composables. Both APIs are fully supported in Vue 3 and compile to the same underlying reactivity, so the choice is about code organization and reuse, not capability.
- Composition API co-locates related logic instead of splitting it by option type
- Logic extracts cleanly into reusable composables
- Better TypeScript inference with the Composition API
- Options API is simpler and more approachable for beginners
- Both share the same reactivity engine and can coexist
AI Mentor Explanation
The Options API is like organizing a team folder by document type: all batting cards in one drawer, all bowling figures in another, all fielding notes in a third. To study one player you open three drawers. The Composition API is a single dossier per player holding their batting, bowling and fielding together, so everything about one concern lives in one place and travels as a unit.
Step-by-Step Explanation
Step 1
Recognize the Options API shape
A component is defined as an object with data, methods, computed and watch options, each grouping code by its kind.
Step 2
Understand the fragmentation problem
As features grow, one concern spreads across several options, forcing you to scroll between them to follow the logic.
Step 3
Move logic into setup()
The Composition API exposes state and behavior from a setup() function using ref, reactive, computed and lifecycle hooks.
Step 4
Group by feature
Keep all state, computed values and functions for a single concern together instead of scattering them by option type.
Step 5
Extract composables
Lift a self-contained block of logic into a useXxx() function so multiple components can reuse it.
What Interviewer Expects
- Clear grasp that both APIs exist in Vue 3 and share one reactivity system
- The code-organization difference: by option type vs by feature
- Awareness of composables for logic reuse
- Knowledge that setup() and <script setup> are the Composition API entry points
- A sense of when each style is appropriate
Common Mistakes
- Claiming the Composition API replaced the Options API in Vue 3
- Saying the Composition API is faster because of the reactivity difference (it uses the same engine)
- Confusing composables with mixins without noting the reuse and naming advantages
- Thinking you must pick one API for the entire codebase
Best Answer (HR Friendly)
“The Options API sorts a component's code into fixed boxes like data and methods, which is easy to start with. The Composition API instead lets you keep all the code for one feature together and share it between components. Both are supported in Vue 3, so teams pick based on how they want to organize and reuse code.”
Code Example
export default {
data() {
return { count: 0 }
},
computed: {
double() {
return this.count * 2
}
},
methods: {
increment() {
this.count++
}
}
}import { ref, computed } from 'vue'
const count = ref(0)
const double = computed(() => count.value * 2)
function increment() {
count.value++
}Follow-up Questions
- What is a composable and how does it differ from a mixin?
- How does <script setup> relate to the Composition API?
- Can you use the Options API and Composition API in the same project?
- Why does the Composition API improve TypeScript support?
- When would you still prefer the Options API?
MCQ Practice
1. How does the Composition API primarily organize component code?
The Composition API groups all logic for a single feature together, unlike the Options API which splits code by option type.
2. Which statement about the two APIs in Vue 3 is correct?
Vue 3 fully supports both APIs, and they compile down to the same underlying reactivity system.
3. What is the main tool for reusing logic across components in the Composition API?
Composables are functions built from Composition API primitives that encapsulate and share stateful logic.
Flash Cards
What organizes code in the Options API? — Fixed option buckets: data, methods, computed and watch.
What organizes code in the Composition API? — Feature-based grouping inside setup() using ref, reactive and computed.
Do both APIs share reactivity? — Yes — both compile to the same Vue 3 reactivity engine.
What replaces mixins for reuse? — Composables — reusable useXxx() functions.