Options API vs Composition API

Beginner · 10 min

The two ways to write Vue components, the same component in each, why Vue 3 added the Composition API, and how to read Options API code in older apps.

Before thisSingle-file components

Program With Erik · 10 min

Options API vs Composition API

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Program With ErikReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • The Options API describes a component as an object with fixed sections (`data`, `computed`, `methods`, `watch`, lifecycle hooks) and reaches everything through `this`; the Composition API declares state and logic as plain variables and functions inside `setup()` or `<script setup>`.
  • The Options API groups code by the kind of option, so one feature ends up spread across several sections; the Composition API lets you group code by feature and lift a feature out into a composable.
  • Logic reuse is the biggest practical difference: Options API reuse relied on mixins, which hide where properties come from and can silently collide, while composables are ordinary functions with explicit inputs and return values.
  • The Composition API works with TypeScript's normal inference because it's plain variables and functions, while the Options API relies on Vue's type helpers to give `this` the right shape.
  • The Options API is not deprecated: it is still fully supported in Vue 3 and is implemented on top of the Composition API, but the official docs and the wider ecosystem now lead with the Composition API and `<script setup>`.
  • Reading older code is mostly a mapping exercise: `data` becomes `ref`/`reactive`, `computed` becomes `computed()`, `methods` become functions, `watch` becomes `watch()`, and `mounted` becomes `onMounted()`.

Two ways to describe the same component

Vue gives you two styles for writing a component’s logic. They render the same templates, use the same reactivity system underneath, and can live side by side in one app. They differ in how you organize code, and that difference matters more the bigger a component gets.

The Options API was Vue’s original style, and the only one in Vue 2. The Composition API arrived with Vue 3 in 2020 (and was backported to Vue 2.7). This roadmap uses the Composition API with <script setup> everywhere else; this lesson exists so that Options API code in an older codebase, a Stack Overflow answer, or a library’s docs reads as familiar rather than foreign.

The same counter, twice

Here is a component with a count, a derived value, a method, a watcher, and a lifecycle hook, in the Options API:

<script>
export default {
  props: {
    step: { type: Number, default: 1 },
  },
  data() {
    return { count: 0 };
  },
  computed: {
    doubled() {
      return this.count * 2;
    },
  },
  methods: {
    increment() {
      this.count += this.step;
    },
  },
  watch: {
    count(newValue) {
      if (newValue > 10) console.log("That's a lot of clicks");
    },
  },
  mounted() {
    console.log("Counter is on the page");
  },
};
</script>

<template>
  <button @click="increment">{{ count }} (doubled: {{ doubled }})</button>
</template>

And the same component with the Composition API in <script setup>:

<script setup>
import { ref, computed, watch, onMounted } from "vue";

const props = defineProps({
  step: { type: Number, default: 1 },
});

const count = ref(0);
const doubled = computed(() => count.value * 2);

function increment() {
  count.value += props.step;
}

watch(count, (newValue) => {
  if (newValue > 10) console.log("That's a lot of clicks");
});

onMounted(() => {
  console.log("Counter is on the page");
});
</script>

<template>
  <button @click="increment">{{ count }} (doubled: {{ doubled }})</button>
</template>

The template is identical. What changed is the script:

  • Structure. The Options API hands you labelled drawers, data, computed, methods, watch, mounted, and you put each piece in the matching drawer. The Composition API is just JavaScript in a scope: you declare things in whatever order makes sense.
  • this. Options API code reaches everything through this, which Vue binds to the component instance. That’s why Options API methods must not be arrow functions, a common bug for anyone unsure how this binds (see the this keyword). The Composition API has no this; count and increment are ordinary variables.
  • .value. A ref wraps a value so Vue can track it, and in script you read and write it through .value. Templates unwrap refs automatically, so the template still says count. Ref and reactive explains why.

Why the Composition API was added

For a counter, the Options API is perfectly pleasant, arguably more so: the drawers tell a beginner where everything goes. The trouble shows up in large components and in shared logic.

Code is grouped by option, not by feature. Imagine a product list with three features: search, pagination, and a “recently viewed” sidebar. In the Options API, the search query sits in data, the filtered list in computed, the debounced fetch in methods, the reaction to a new query in watch, and the initial load in mounted, and pagination and “recently viewed” are split across the same five sections. To understand one feature you scroll through all of them. It’s like a toolbox sorted by colour instead of by job: everything has a place, but the tools for one task are spread across every drawer.

With the Composition API, each feature’s state, derived values, watchers, and hooks can sit together:

// search, all in one place
const query = ref("");
const results = ref([]);
watch(query, debounce(async (q) => {
  results.value = await searchProducts(q);
}, 300));

// pagination, all in one place
const page = ref(1);
const pageCount = computed(() => Math.ceil(results.value.length / 20));

Reuse was awkward. The Options API’s main reuse tool was the mixin, an options object merged into a component. Mixins have three well-known problems. Properties appear on this with no visible source, so in a component using three mixins you can’t tell where isLoading came from. Two mixins defining the same name collide. And a mixin can’t easily take arguments. The Composition API’s answer is the composable: since the search logic above is just JavaScript, you can move it into a function and call it.

// composables/useSearch.js
export function useSearch(fetcher) {
  const query = ref("");
  const results = ref([]);
  watch(query, debounce(async (q) => {
    results.value = await fetcher(q);
  }, 300));
  return { query, results };
}

// in any component
const { query, results } = useSearch(searchProducts);

The data flow is explicit: you pass inputs in and destructure exactly what comes back. Composables get a full lesson in composables. React developers will recognize the idea: it plays the same role as custom hooks, though Vue’s setup runs once per component instance rather than on every render.

Types come for free. Because Composition API code is plain variables and functions, TypeScript infers types the normal way. The Options API depends on Vue’s typing helpers working out the shape of this from all the option sections, which works but produces harder error messages.

Is the Options API going away?

No. Vue 3 fully supports it, and the core team has said they have no plans to remove it. Internally, the Options API is implemented on top of the Composition API. If an app doesn’t use it at all, a build can set the __VUE_OPTIONS_API__ compile-time flag to false to drop that layer and save a few kilobytes.

What has changed is the default. The official docs lead with the Composition API, the major libraries (Vue Router, Pinia, VueUse) are built around composables, and most new examples you’ll find use <script setup>. For new code, use the Composition API; for existing code, the Options API is something to read fluently and migrate when there’s a reason to. The two can even mix: an Options API component can include a setup() option that returns Composition API state, which is a common halfway step when migrating.

Reading Options API code: a translation table

Options API Composition API (<script setup>)
props: { ... } const props = defineProps({ ... })
emits: [...] and this.$emit("x") const emit = defineEmits([...]) and emit("x")
data() { return { a: 1 } } const a = ref(1) or reactive({ a: 1 })
computed: { b() { ... } } const b = computed(() => ...)
methods: { c() { ... } } function c() { ... }
watch: { a(val) { ... } } watch(a, (val) => { ... })
created() code at the top level of <script setup>
mounted(), unmounted() onMounted(...), onUnmounted(...)
this.$refs.input const input = useTemplateRef("input") (Vue 3.5+)
mixins composables

Each right-hand entry has its own lesson later in the roadmap. If you meet Vuex alongside Options API components, that combination is covered in Vuex and migrating to Pinia.

What the video covers

Erik Hanchett builds the same small component four ways: Options API, Composition API with a setup() function, <script setup>, and a hybrid that mixes the two. It’s the quickest way to see the shapes side by side. The title argues you don’t need the Composition API, which is true for small components; the sections above explain why the docs still default to it for real apps. For the full debate, including what the Vue team has said about the Options API’s future, Alexander Lichter’s video in the links below goes through the arguments on both sides.

Check yourself

  1. 01In the Options API, why can a single feature such as "search" end up scattered across a component?

    The Options API organizes code by option type, so the search query lives in `data`, the filtered results in `computed`, the debounced fetch in `methods`, the reaction to query changes in `watch`, and the initial load in `mounted`. Every feature in the component is split the same way, so in a large component the pieces of one feature are interleaved with pieces of every other feature.

  2. 02What problems do mixins have that composables avoid?

    A mixin merges its options into the component, so in the template or in `this` you can't tell which mixin a property came from; two mixins defining the same property name collide; and mixins can't easily be parameterized. A composable is a function you call explicitly, you destructure exactly what it returns (renaming if needed), and you can pass it arguments, so the data flow is visible.

  3. 03Is it safe to start a new Vue 3 project with the Options API?

    It is supported and will keep working; Vue's maintainers have said they have no plans to remove it. But the official docs, most current libraries, and most new examples use the Composition API with `<script setup>`, and logic reuse and TypeScript are easier there. For new code the Composition API is the better default; the Options API is mainly something to be able to read and maintain.

  4. 04How would you translate an Options API `mounted() { this.fetchUser(); }` hook to `<script setup>`?

    Import `onMounted` from `vue`, declare `fetchUser` as an ordinary function, and register the hook with `onMounted(() => { fetchUser(); })`. There is no `this`; the function is called directly because it is a variable in the same scope.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark