In Vue.js, there are two different syntaxes for creating components: the Options API and the Composition API.
- The Options API is the traditional Vue.js approach
- The Composition API is a more modern and flexible approach, introduced in Vue.js 3 💖
Both syntaxes can coexist in the same project. They are fully compatible. However, generally, we choose one or the other (to avoid confusion).
In this course, we will focus mainly on the Composition API because it is the modern and recommended way (and also, it’s the one I like).
But you need to know about both, especially because you will still encounter it in many tutorials and libraries.
So let’s get started 👇.
Comparison Options API vs Composition API
Let’s start by looking at the advantages and disadvantages of each syntax. In summary.
| Feature | Options API | Composition API |
|---|---|---|
| Organization | By options (data, methods) | By functionality (grouping) |
| Simplicity | Simpler | More complex |
| Flexibility | Limited | High |
| Reusability | Difficult (mixins) | Easy (composables) |
| Use in projects | Small or medium | Large or complex |
| Compatibility | Vue 2 and 3 | Vue 3 (recommended) |
In general, the logic and organization of the new Composition API is better. But, the traditional Optional API syntax can be more intuitive for developers new to Vue.js.
Personally, I don’t see the Optional API as being much simpler. I recommend going directly for the Composition API 🤷.
What is the Composition API?
The Composition API is the new syntax introduced in Vue.js 3, which is more modern and more flexible (introduced to address limitations of the Options Api)
In this approach, logic is organized into reusable functions, which allows for better code organization and greater logic reuse.
<template>
<div>
<p>Counter: {{ count }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0);
function increment() {
count.value++;
}
</script>
In this example,
- The component’s logic is organized in functions.
- The state (
count) and methods (increment) are defined insidesetup.
What is the Options API?
The Options API is the classic syntax of Vue.js for creating components in Vue.js, which can be more intuitive for beginners.
In this approach, the logic is organized into clearly defined and separated options, such as data, methods, computed, watch, and props, etc…
<template>
<div>
<p>Counter: {{ count }}</p>
<button @click="increment">Increment</button>
</div>
</template>
<script>
export default {
data() {
return {
count: 0,
};
},
methods: {
increment() {
this.count++;
},
},
};
</script>
In this example,
- The component’s logic is organized in the
dataandmethodsoptions - The state (
count) and methods (increment) are defined inside these options.
Migrate from Options API to Composition API
If at any point you need to migrate a Vue component from Options API to Composition API, here are some guidelines on how to do it (and with patience).
First, import the functions you need from vue, such as ref, reactive, computed, and watch:
import { ref, reactive, computed, watch } from 'vue'
Now you will have to replace options like data, methods, computed and watch with their equivalents,
| Options in Options API | Migration to Composition API |
|---|---|
| data | Move properties to ref or reactive |
| methods | Define functions directly in <script setup> |
| computed | Use computed() to define computed properties |
| watch | Use watch() to observe changes |
| props | Use defineProps() to define properties |
| emit | Use defineEmits() to emit events |
| mounted, created, etc. | Use hooks onMounted() and onUnmounted() |
<script>
export default {
data() {
return {
count: 0
}
},
computed: {
doubleCount() {
return this.count * 2
}
},
methods: {
increment() {
this.count++
}
}
}
</script>
<script setup>
import { ref, computed } from 'vue'
const count = ref(0)
const doubleCount = computed(() => count.value * 2)
function increment() {
count.value++
}
</script>
You don’t need to migrate the entire project at once. You can migrate individual components as you need them.
