Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 87 ยท Lifecycle and Composition API

The Options API

Learn the Vue Options API - data, methods, computed, watch and lifecycle options, how this works, the pitfalls of arrow functions, and how every option maps to the Composition API.

Vue has two ways to write a component. The Composition API, which this tutorial uses, groups code by feature. The Options API is the older style: you write one object and put each kind of code under a fixed key. State goes under data, functions under methods, derived values under computed, and so on. Vue 2 only had this style, so a huge amount of existing Vue code, documentation and Stack Overflow answers use it.

You do not need to write Options API components in new projects, but you do need to read them. Both styles run on the same reactivity engine, they can live side by side in one app, and every Options concept has a direct Composition equivalent. This lesson walks through the options, explains this, and ends with a mapping table you can keep next to you.

data, methods and computed

A component object has a handful of well-known keys. The first three cover most components.

An Options API shopping line

The preview starts with "2 x Notebook = $9.00" and updates as you press the buttons.

  • data is a function that returns an object. Each property becomes reactive state. It must be a function (not a plain object) so every component instance gets its own copy.
  • computed holds derived values written as functions. Like in the Composition API they are cached and recalculate only when what they read changes. Here summary reads the computed total.
  • methods holds functions you call from the template or from other code. Vue binds each one to the component, so @click="add" works without extra effort.

Everything meets in the template: quantity, summary and add are all available by name, whichever option they came from.

this: the component instance

Inside data, computed, methods and the other options, this is the component instance. It is a proxy that gives you one flat view over everything the component has: data properties, props, computed values and methods all appear as properties of this. Writing this.quantity++ updates reactive data; this.add() calls a method; this.$emit("change") sends an event; this.$refs.input reaches a template ref.

flowchart LR
    P["props"] --> T["this"]
    D["data"] --> T
    C["computed"] --> T
    M["methods"] --> T
    T --> TPL["template: {{ }} and @click"]

Props, data, computed values and methods are merged into one this, which is also the template's scope.

This convenience has two consequences. First, names must be unique across those groups: a method and a data property with the same name would clash. Second, this is not magic JavaScript, it depends on how the function is called, and that leads to the best-known Options API pitfall.

Do not use arrow functions for options

An arrow function does not have its own this; it uses the this of the place where it was written. At the top level of a module that is undefined. So a methods entry written as an arrow function cannot reach the component.

Why arrow functions break this

"Good method" raises the count. "Arrow method" does not, and the line under the app shows the engine's complaint: Cannot read properties of undefined (reading 'count'). The shorthand goodIncrement() { ... } and function () { ... } are fine. Use them for methods, computed getters, and the lifecycle options. Arrow functions are fine inside a method, for example in items.map((item) => item * this.factor), because there they inherit the method's this.

The same rule applies to the data option. Writing data: () => ({ ... }) works as long as it never needs this, but it cannot read other properties of the instance. Stay with data() { return {...} } and you never have to think about it.

watch and lifecycle options

Watchers are declared in the watch option as a map from the name of a property to a handler. Lifecycle hooks are options named after the stage: created, mounted, updated, unmounted and friends. They are the same moments you met in Lifecycle hooks.

watch, created and mounted as options

The history starts with "created: data exists, no DOM yet" and "mounted: DOM exists, field tag is input"; each keystroke adds an "old -> new" line. A few more details about the watch option:

  • A watcher handler receives (newValue, oldValue).
  • To watch a nested property use a dotted string key: "user.name"(value) { ... }.
  • For options such as deep or immediate, write the entry as an object: name: { handler(value) { ... }, immediate: true }.
  • The created hook is where Vue 2 code usually kicked off data loading. In the Composition API that code lives directly in setup().

Mapping to the Composition API

The two styles describe the same component. Here is the first example rewritten with setup(), placed next to the Options version's pieces so you can see each one move.

The same component with the Composition API

The output is identical. What changed is where things live and how you reach them.

Options API Composition API
data() { return { n: 0 } } const n = ref(0) (or reactive)
computed: { total() {} } const total = computed(() => ...)
methods: { add() {} } function add() {}
watch: { n(v) {} } watch(n, (v) => {})
created() code directly in setup()
mounted(), updated(), unmounted() onMounted(), onUpdated(), onUnmounted()
props, emits options props/emits options or defineProps/defineEmits
this.$refs.field a ref named in the template
this.$emit("x") emit("x") from setup(props, { emit })
mixins: [m] composables
flowchart LR
    subgraph O["Options API"]
      direction TB
      o1["data"]
      o2["computed"]
      o3["methods"]
      o4["watch"]
      o5["mounted"]
    end
    subgraph C["Composition API"]
      direction TB
      c1["ref / reactive"]
      c2["computed()"]
      c3["functions"]
      c4["watch()"]
      c5["onMounted()"]
    end
    o1 --> c1
    o2 --> c2
    o3 --> c3
    o4 --> c4
    o5 --> c5

The same ideas, spelled differently.

You can even combine them. A component with Options API keys can also have a setup() function; what setup() returns becomes available on this too. That makes gradual migration possible: move one feature at a time into setup() or a composable.

When you will meet the Options API

  • Older codebases and tutorials. Anything written for Vue 2 uses it. Vue 3 still supports it fully; Vue's own docs state that both APIs are first-class.
  • Libraries and examples that favor a small footprint, such as a quick createApp({ data() {...} }) on a plain HTML page.
  • Simple components. For a small, mostly presentational component, the fixed places for code can be easier for beginners.
  • Migration projects. Vue 2 to Vue 3 upgrades keep the Options API, and teams refactor to the Composition API later, one component at a time.

For new code, the Composition API with <script setup> (Composition API and script setup) brings better logic reuse through composables and better TypeScript support. The choice is not either-or: knowing both lets you read and maintain any Vue project.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why must data be a function in a component?

    If data were one shared object, every instance of the component would share and mutate the same state. A function creates a fresh object per instance.
  2. 2. What is wrong with writing a method as an arrow function in the methods option?

    An arrow function takes this from where it was written, which is undefined at the top level of a module. Methods need the regular method syntax so Vue can bind this to the component.
  3. 3. Which Composition API feature replaces mixins from the Options API?

    Composables share logic through plain functions, and every value they provide is visible where you call them, unlike mixins that merge in invisibly.
  4. 4. Where does the code from the created hook usually go in a setup() component?

    There is no onCreated. setup() runs at the same stage as created, so that code simply goes in the setup function body.

Summary

  • The Options API organizes a component by option type: data, computed, methods, watch and lifecycle hooks like created and mounted.
  • data is a function returning the state object, so each instance has its own state.
  • this is a proxy exposing props, data, computed values and methods; use regular method syntax, never arrow functions, for options.
  • Each option maps directly to a Composition API feature: ref, computed, plain functions, watch, and the onX hooks.
  • Both styles can be mixed in one component and share the same reactivity.
  • Learn to read the Options API because many projects, libraries and answers online still use it; write new code with <script setup>.

Next, let elements animate as they appear and disappear: The Transition component.