vuejs-options-api-vs-composition-api

Options API and Composition API in Vue.js

  • 4 min

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.

FeatureOptions APIComposition API
OrganizationBy options (data, methods)By functionality (grouping)
SimplicitySimplerMore complex
FlexibilityLimitedHigh
ReusabilityDifficult (mixins)Easy (composables)
Use in projectsSmall or mediumLarge or complex
CompatibilityVue 2 and 3Vue 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>
Copied!

In this example,

  • The component’s logic is organized in functions.
  • The state (count) and methods (increment) are defined inside setup.

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>
Copied!

In this example,

  • The component’s logic is organized in the data and methods options
  • 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'
Copied!

Now you will have to replace options like data, methods, computed and watch with their equivalents,

Options in Options APIMigration to Composition API
dataMove properties to ref or reactive
methodsDefine functions directly in <script setup>
computedUse computed() to define computed properties
watchUse watch() to observe changes
propsUse defineProps() to define properties
emitUse 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>
Copied!
<script setup>
import { ref, computed } from 'vue'

const count = ref(0)

const doubleCount = computed(() => count.value * 2)

function increment() {
  count.value++
}
</script>
Copied!

You don’t need to migrate the entire project at once. You can migrate individual components as you need them.