0
0
Vueframework~10 mins

Options API vs Composition API decision in Vue - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Options API vs Composition API decision
Start: Choose API style
Options API
Define data, methods, computed
Component created
Use template with this
Render component
End
Start: Choose API style
Composition API
Import reactive, ref, functions
Setup function: define state and logic
Return reactive state
Use template with direct refs
Render component
End
This flow shows the two main ways to build Vue components: Options API organizes code by options like data and methods, while Composition API organizes by logical features inside a setup function.
Execution Sample
Vue
export default {
  data() { return { count: 0 } },
  methods: { increment() { this.count++ } }
}
This Options API example defines a count state and an increment method inside a Vue component.
Execution Table
StepActionState BeforeState AfterOutput/Effect
1Component createdcount: undefinedcount: 0Initial state set
2Call increment methodcount: 0count: 1count increased by 1
3Render templatecount: 1count: 1Display count = 1
4Call increment method againcount: 1count: 2count increased by 1
5Render templatecount: 2count: 2Display count = 2
6Endcount: 2count: 2Component stable
💡 Component lifecycle ends or user stops interaction
Variable Tracker
VariableStartAfter 1After 2Final
countundefined012
Key Moments - 2 Insights
Why do we use 'this.count' in Options API but just 'count' in Composition API?
In Options API, 'this' refers to the component instance, so data and methods are accessed via 'this'. In Composition API, variables are defined inside setup and returned, so you use them directly without 'this'. See execution_table steps 2 and 3.
How does Composition API help with organizing complex logic better than Options API?
Composition API groups related code (state, functions) together inside setup, making it easier to manage and reuse. Options API splits code by type (data, methods), which can scatter related logic. This is why many prefer Composition API for bigger components.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'count' after step 4?
A1
B0
C2
Dundefined
💡 Hint
Check the 'State After' column for step 4 in the execution_table.
At which step does the component first render the count value?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look for 'Render template' action in the execution_table.
If we switch to Composition API, how would accessing 'count' in the template change?
AUse 'count' directly without 'this'
BUse 'count.value' inside template
CUse 'this.count' as before
DCannot access 'count' in template
💡 Hint
Recall key_moments explanation about 'this' usage difference between APIs.
Concept Snapshot
Options API:
- Organizes code by options: data, methods, computed
- Uses 'this' to access state and methods
- Simple for small components

Composition API:
- Organizes code by logical features inside setup()
- Uses refs/reactive for state
- Access state directly without 'this'
- Better for complex, reusable logic
Full Transcript
This visual guide compares Vue's Options API and Composition API. Options API organizes component code by options like data and methods, accessed via 'this'. Composition API uses a setup function to group related state and logic, accessed directly without 'this'. The execution table traces a simple count increment example showing state changes and rendering steps. Key moments clarify why 'this' is used in Options API but not in Composition API, and how Composition API improves code organization. The quiz tests understanding of state changes and API differences. The snapshot summarizes key syntax and usage differences for quick reference.