Vue offers two ways to write components: Options API and Composition API. Choosing between them helps you organize your code better and work more easily.
Options API vs Composition API decision in Vue
Options API: export default { data() { return { count: 0 }; }, methods: { increment() { this.count++; } } } Composition API: import { ref } from 'vue'; export default { setup() { const count = ref(0); function increment() { count.value++; } return { count, increment }; } }
The Options API groups code by options like data, methods, and computed.
The Composition API groups code by feature or logic using functions inside setup().
export default { data() { return { message: 'Hello' }; }, methods: { greet() { alert(this.message); } } }
setup().import { ref } from 'vue'; export default { setup() { const message = ref('Hello'); function greet() { alert(message.value); } return { message, greet }; } }
This is a simple Vue component using the Composition API. It shows a count and a button to increase it. The count updates reactively when you click the button.
<template>
<div>
<p>Count: {{ count }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0);
function increment() {
count.value++;
}
</script>The Options API is easier for beginners and small projects.
The Composition API is better for complex logic and code reuse.
You can mix both APIs in the same project if needed.
The Options API organizes code by options like data and methods.
The Composition API organizes code by logical features inside setup().
Choose based on project size, complexity, and your comfort with Vue.
Practice
data, methods, and computed?Solution
Step 1: Understand Options API structure
The Options API groups code by options such asdata,methods, andcomputed.Step 2: Compare with Composition API
The Composition API organizes code inside thesetup()function by logical features, not by options.Final Answer:
Options API -> Option AQuick Check:
Options API groups by options like data and methods [OK]
- Confusing Composition API with Options API structure
- Thinking both APIs organize code the same way
- Believing Options API uses setup() function
Solution
Step 1: Identify Composition API reactive declaration
In Composition API, reactive state usesref()insidesetup(), e.g.,const count = ref(0);.Step 2: Compare other options
data() { return { count: 0 } } is Options API syntax, A and D are for methods and computed properties, not reactive refs.Final Answer:
const count = ref(0); inside setup() -> Option AQuick Check:
Use ref() in setup() for reactive state [OK]
- Using data() syntax in Composition API
- Confusing methods with reactive state
- Trying to declare reactive state outside setup()
import { ref } from 'vue';
export default {
setup() {
const count = ref(0);
function increment() {
count.value++;
}
return { count, increment };
}
}What will be the value of
count after calling increment() twice?Solution
Step 1: Understand ref and increment function
countis a ref initialized to 0. Callingincrement()increasescount.valueby 1 each time.Step 2: Calculate count after two increments
Starting at 0, after two increments: 0 + 1 + 1 = 2.Final Answer:
2 -> Option CQuick Check:
Two increments add 2 to count.value [OK]
- Forgetting to use .value with ref
- Assuming count is a plain number, not a ref
- Thinking increment does not change count
export default {
data() {
count: 0
},
methods: {
increment() {
this.count++;
}
}
}Solution
Step 1: Check data() function syntax
In Options API,data()must return an object. Here, it lacksreturnkeyword.Step 2: Verify other parts
Method name and usage ofthis.countare correct.data()is correctly a function.Final Answer:
Missing return statement in data() -> Option DQuick Check:
data() must return an object [OK]
- Omitting return in data()
- Confusing this.count with count
- Thinking data() can be an object directly
Solution
Step 1: Consider project size and logic reuse
Large projects with shared logic benefit from grouping related code together for reuse and clarity.Step 2: Compare API strengths
Composition API allows grouping logic insidesetup(), making reuse easier. Options API separates by options, which can scatter related logic.Final Answer:
Composition API, because it groups related logic together for better reuse -> Option BQuick Check:
Composition API groups logic for reuse in large projects [OK]
- Choosing Options API for better reuse in large projects
- Thinking Composition API avoids setup()
- Believing Options API is more modern
Ready to prove it?
Pass the test and this pattern moves unknown
