Loading...
Loading...
Vue.js offers two ways to write components: Options API and Composition API.
data, methods, computed, watch).setup(), ref(), reactive(), computed(), watch()).In Options API code is organized as object with options:
data — datamethods — methodscomputed — computed propertieswatch — watchersmounted, created — lifecycle hooks<template>
<p>Counter: {{ count }}</p>
<button @click="increment">+</button>
</template>
<script>
export default {
data() {
return {
count: 0,
};
},
methods: {
increment() {
this.count++;
},
},
};
</script>
Composition API allows writing code inside setup(), using ref(), reactive(), computed(), watch.
<template>
<p>Counter: {{ count }}</p>
<button @click="increment">+</button>
</template>
<script setup>
import { ref } from 'vue';
const count = ref(0);
const increment = () => count.value++;
</script>
| Criterion | Options API | Composition API |
|---|---|---|
| Simple components | Yes | Yes |
| Large projects | Can be complex | Flexible |
| Reusable logic | Difficult | Convenient |
| Performance | Fast | Fast |
| Vue 2 support | Yes | Only via @vue/composition-api |