In the previous lessons you have seen Vue code that looks two different ways: sometimes with data(), methods and this, and sometimes with ref, standalone functions and <script setup>. This is not an oversight: Vue offers two API styles for writing a component's logic, the Options API and the Composition API, and the latter can in turn be written either with the setup() function or with the <script setup> syntax. All three produce exactly the same result on screen. In this lesson we will write the same Colmena Bookings component in all three variants, compare their equivalents piece by piece, analyse what each style offers and where you will come across it, and finish with the approach the course will follow.

Contents

  1. The example component: a simplified room card
  2. Version 1: Options API
  3. Version 2: Composition API with setup()
  4. Version 3: Composition API with <script setup>
  5. Equivalence table
  6. What each style offers
  7. Where you will find each one, and whether they can be mixed
  8. The course's approach

  1. The example component: a simplified room card

We are going to create a first draft of RoomCard.vue, the component that will display each Colmena room. In module 3 we will turn it into a reusable component that receives the room from outside; for now, the room will be written inside the component itself.

Requirements for this version:

  • It displays the details of the Panal Room (floor 1, 8 people, €25/h).
  • It has an attendee counter with buttons to add and remove attendees, never going below 0 or above the capacity.
  • It calculates the free seats (capacity minus attendees).
  • When the room becomes full, it shows a notice with the time it filled up; if a seat is freed, the notice disappears.
  • When it is mounted, it writes a message to the console.

These requirements force us to use all the usual ingredients of a component: state, computed values, functions, a watcher and a lifecycle hook. The template and styles will be identical in all three versions; only the <script> block will change:

<template>
  <article class="card">
    <h2>{{ room.name }}</h2>
    <p>Floor {{ room.floor }} · €{{ room.hourlyPrice }}/h</p>
    <p>Attendees: {{ attendees }} / {{ room.capacity }}</p>
    <p>Free seats: <strong>{{ freeSeats }}</strong></p>
    <p class="notice">{{ notice }}</p>
    <button @click="removeAttendee">− Attendee</button>
    <button @click="addAttendee">+ Attendee</button>
  </article>
</template>

<style scoped>
.card {
  max-width: 320px;
  padding: 1rem 1.5rem;
  border: 2px solid var(--color-honey);
  border-radius: 10px;
  background: white;
}

.notice {
  min-height: 1.5em; /* reserves the space even when there is no notice */
  color: #b00020;
  font-weight: bold;
}
</style>

While there is no notice, the .notice paragraph stays empty. Once you learn v-if in Conditional Rendering, you will be able to avoid rendering it at all.

  1. Version 1: Options API

The Options API is Vue's classic style (the only one that existed in Vue 2). The component is an object with predefined options, and each kind of thing goes in its own option: state in data, computed values in computed, functions in methods, and so on.

<!-- src/components/RoomCard.vue (Options API version) -->
<script>
export default {
  name: 'RoomCard',

  // State: everything data() returns is reactive
  data() {
    return {
      room: {
        id: 'panal',
        name: 'Panal Room',
        floor: 1,
        capacity: 8,
        hourlyPrice: 25,
        equipment: ['projector', 'whiteboard'],
        available: true
      },
      attendees: 0,
      notice: ''
    }
  },

  // Values derived from the state; they recalculate themselves
  computed: {
    freeSeats() {
      return this.room.capacity - this.attendees
    },
    full() {
      return this.freeSeats === 0
    }
  },

  // Watchers: react to changes with side effects
  watch: {
    full(isFull) {
      this.notice = isFull
        ? `Room full as of ${new Date().toLocaleTimeString('en-GB')}!`
        : ''
    }
  },

  // Functions used by the template
  methods: {
    addAttendee() {
      if (!this.full) this.attendees++
    },
    removeAttendee() {
      if (this.attendees > 0) this.attendees--
    }
  },

  // Lifecycle hook
  mounted() {
    console.log(`[RoomCard] ${this.room.name} ready`)
  }
}
</script>

Key points:

  • export default { ... }: the component is an exported options object.
  • data(): a function that returns a new object for each instance (so if you use the card twice, each one has its own counter).
  • computed: freeSeats and full are calculated from the state, and Vue caches them until their dependencies change. We will go deeper into them in Computed Properties and Watchers.
  • watch: when full changes value, the function runs. We use a watcher (rather than a computed property) because we want to record the moment the room filled up, which cannot be derived from the current state alone.
  • this: in every option, this is the component instance and gives equal access to data, computed properties and methods.

  1. Version 2: Composition API with setup()

The Composition API arrived with Vue 3. Instead of spreading the code across options, we write the logic inside a setup() function using functions imported from vue (ref, computed, watch, onMounted...) and return whatever the template needs.

<!-- src/components/RoomCard.vue (Composition API version with setup) -->
<script>
import { ref, computed, watch, onMounted } from 'vue'

export default {
  name: 'RoomCard',

  setup() {
    // The room does not change in this component: a plain object is enough
    const room = {
      id: 'panal',
      name: 'Panal Room',
      floor: 1,
      capacity: 8,
      hourlyPrice: 25,
      equipment: ['projector', 'whiteboard'],
      available: true
    }

    // Reactive state
    const attendees = ref(0)
    const notice = ref('')

    // Computed values
    const freeSeats = computed(() => room.capacity - attendees.value)
    const full = computed(() => freeSeats.value === 0)

    // Watcher
    watch(full, (isFull) => {
      notice.value = isFull
        ? `Room full as of ${new Date().toLocaleTimeString('en-GB')}!`
        : ''
    })

    // Functions
    function addAttendee() {
      if (!full.value) attendees.value++
    }

    function removeAttendee() {
      if (attendees.value > 0) attendees.value--
    }

    // Lifecycle hook
    onMounted(() => {
      console.log(`[RoomCard] ${room.name} ready`)
    })

    // Whatever we return becomes available in the template
    return { room, attendees, notice, freeSeats, addAttendee, removeAttendee }
  }
}
</script>

Differences from the previous version:

  • There is no this. Everything is ordinary JavaScript variables and functions inside setup().
  • You decide what is reactive. In the Options API everything in data is reactive; here room is a plain object because it does not change, and only attendees and notice use ref. (When a whole object needs to be reactive there is reactive, which you will see in Reactivity: ref and reactive.)
  • .value: refs and computeds are read with .value in the script (but not in the template).
  • computed(() => ...) and watch(source, callback) are functions, not options.
  • The return is mandatory: anything not returned does not exist as far as the template is concerned. Notice that full is not returned because the template does not use it; it still works internally.

  1. Version 3: Composition API with <script setup>

<script setup> is a shorthand for the previous version that the Vue compiler turns into a setup() function. It is the officially recommended syntax for components in .vue files, and the one you have already used in App.vue and ReceptionClock.vue:

<!-- src/components/RoomCard.vue (<script setup> version) -->
<script setup>
import { ref, computed, watch, onMounted } from 'vue'

// The room does not change in this component: a plain object is enough
const room = {
  id: 'panal',
  name: 'Panal Room',
  floor: 1,
  capacity: 8,
  hourlyPrice: 25,
  equipment: ['projector', 'whiteboard'],
  available: true
}

// Reactive state
const attendees = ref(0)
const notice = ref('')

// Computed values
const freeSeats = computed(() => room.capacity - attendees.value)
const full = computed(() => freeSeats.value === 0)

// Watcher: records the moment the room fills up
watch(full, (isFull) => {
  notice.value = isFull
    ? `Room full as of ${new Date().toLocaleTimeString('en-GB')}!`
    : ''
})

// Functions
function addAttendee() {
  if (!full.value) attendees.value++
}

function removeAttendee() {
  if (attendees.value > 0) attendees.value--
}

// Lifecycle hook
onMounted(() => {
  console.log(`[RoomCard] ${room.name} ready`)
})
</script>

What disappears compared with version 2:

  • export default { setup() { ... } }: the content of the block is the setup.
  • The return: everything declared at the top level (variables, functions and imports, including imported components) is automatically available in the template.
  • The name option: the component's name is inferred from the file name (RoomCard.vue).

To try out any of the three versions, save one of them in src/components/RoomCard.vue (the script plus the template and styles from section 1) and use it in App.vue:

<script setup>
import { ref } from 'vue'
import ReceptionClock from './components/ReceptionClock.vue'
import RoomCard from './components/RoomCard.vue'

// ... rest of the script as in the previous lesson ...
</script>

<template>
  <header class="header">
    <!-- ... as before ... -->
  </header>

  <main class="content">
    <!-- ... the counter and buttons from before ... -->
    <RoomCard />
  </main>
</template>

Click "+ Attendee" eight times: the free seats reach 0 and the notice appears with the time. Click "− Attendee" and the notice disappears. In Vue DevTools, when you select RoomCard, you will see its state grouped slightly differently depending on the version (data/computed in the Options API, setup in the Composition API), but the behaviour is identical. Keep the <script setup> version in the project.

  1. Equivalence table

This table will help you translate mentally between styles, something you will do often when reading documentation or existing code:

Concept Options API Composition API (setup() / <script setup>)
Reactive state data() { return { attendees: 0 } } const attendees = ref(0) or reactive({ ... }) for objects
Accessing state in the script this.attendees attendees.value (with ref)
Computed values computed: { freeSeats() { ... } } const freeSeats = computed(() => ...)
Functions methods: { addAttendee() { ... } } function addAttendee() { ... }
Watchers watch: { full(newValue, oldValue) { ... } } watch(full, (newValue, oldValue) => { ... })
Lifecycle hooks mounted() { ... }, unmounted() { ... } onMounted(() => { ... }), onUnmounted(() => { ... })
Instance creation beforeCreate(), created() The setup code itself
Props props: ['room'] → this.room const props = defineProps(['room']) in <script setup>; first argument of setup(props)
Emitted events emits: ['book'] → this.$emit('book') const emit = defineEmits(['book']) → emit('book')
Exposure to the template Automatic (everything in the options) return { ... } in setup(); automatic in <script setup>
Component name name option name in setup(); the file name in <script setup>

You will study props and events in detail in Props and Custom Events; here you only need to recognise them.

  1. What each style offers

6.1 Organisation: by option versus by feature

The deepest difference is not one of syntax but of organisation. Imagine the card grows and, as well as occupancy, it tracks whether the room is one of the member's favourites. In the Options API, the code for each feature ends up spread across options:

export default {
  data() {
    return {
      attendees: 0,      // occupancy
      notice: '',        // occupancy
      isFavorite: false  // favourites
    }
  },
  computed: {
    freeSeats() { /* occupancy */ },
    full() { /* occupancy */ }
  },
  watch: {
    full() { /* occupancy */ }
  },
  methods: {
    addAttendee() { /* occupancy */ },
    removeAttendee() { /* occupancy */ },
    toggleFavorite() { /* favourites */ }
  }
}

In the Composition API, the code is grouped by feature: everything to do with occupancy together, everything to do with favourites together.

// --- Occupancy ---
const attendees = ref(0)
const notice = ref('')
const freeSeats = computed(() => room.capacity - attendees.value)
const full = computed(() => freeSeats.value === 0)
watch(full, (isFull) => { /* ... */ })
function addAttendee() { /* ... */ }
function removeAttendee() { /* ... */ }

// --- Favourites ---
const isFavorite = ref(false)
function toggleFavorite() {
  isFavorite.value = !isFavorite.value
}
flowchart LR
    subgraph OPT["Options API: grouped by option type"]
        D["data: occupancy + favourites"]
        C["computed: occupancy"]
        W["watch: occupancy"]
        M["methods: occupancy + favourites"]
    end
    subgraph COMP["Composition API: grouped by feature"]
        A["Occupancy block: state, computed, watch, functions"]
        F["Favourites block: state and functions"]
    end

In small components you will barely notice; in components hundreds of lines long, with five or six responsibilities, the difference in readability is huge.

6.2 Reusing logic

Because Composition API logic is made of ordinary functions, it can be extracted into a separate function and reused across several components. These functions are called composables and, by convention, their names start with use. For example, the occupancy logic could become something like this:

// A composable in the making (we will develop this in module 9)
export function useOccupancy(capacity) {
  const attendees = ref(0)
  const freeSeats = computed(() => capacity - attendees.value)
  return { attendees, freeSeats }
}

In the Options API, logic was reused through mixins, which cause naming conflicts and make it hard to tell where each property comes from. Composables are covered in depth in The Vue 3 Composition API in Depth: Composables and Logic Reuse.

6.3 Overall comparison

Aspect Options API Composition API with <script setup>
Getting started Very easy: guided structure, "a place for everything" Slightly harder: you need to understand ref, .value and reactivity
Organisation By option type By feature
Reusing logic Mixins (problematic) Composables (ordinary functions)
TypeScript type inference Limited; needs extra help Excellent: they are ordinary variables and functions (09-02)
Generated code Larger; property names cannot be minified More compact and minifies better
Use of this Constant (a frequent source of bugs with arrow functions) Not used
Status in Vue 3 Fully supported; not deprecated Recommended for new projects

  1. Where you will find each one, and whether they can be mixed

You will find the Options API in:

  • Legacy Vue 2 code and projects migrated to Vue 3 without rewriting their components.
  • A great deal of documentation, tutorials and forum answers from before 2022.
  • Teams that prefer it for its guided structure in small or medium-sized projects.

You will find the Composition API in:

  • The vast majority of new projects and in the current documentation (which lets you switch between the two styles with a toggle).
  • The internal code of libraries in the ecosystem (Vue Router, Pinia, VueUse...), which expose their features as composables: useRouter(), useRoomsStore()...
  • Projects that use TypeScript.

Can they be mixed? Yes, at two levels:

  • Between components: completely. The same application can have some components written with the Options API and others with <script setup>; they are nested inside one another with no difference at all. This makes gradual migration possible.
  • Within a component: an Options API component can have a setup() option whose result is accessible through this in the other options (but not the other way round). This is useful for using a composable in an older component, but it is not recommended as a regular style: it forces you to think in two models at once.

  1. The course's approach

In this course, and therefore in Colmena Bookings:

  • <script setup> is the default style for every component. It is the officially recommended one, the most concise, the one that integrates best with TypeScript and the one used by the ecosystem's modern tools.
  • We will show the Options API equivalent where it adds value: for example, when studying computed properties or the lifecycle, or in the Vuex module, where much of the existing code uses it. That way you will be able to work confidently on legacy projects.
  • We will use the setup() form only when it is necessary, as in examples without a compiler (CDN) or when adding new logic to Options API components.

Common Mistakes and Tips

  • Mixing styles within the same block. Writing this.attendees in <script setup>, or attendees.value inside methods, does not work. First work out which style the component is written in.
  • Forgetting the return in setup(). The template will show warnings about undefined properties. In <script setup> you do not need it.
  • Making everything a ref "just in case". If a value does not change (like room in this draft), a plain object or constant is enough and clearer.
  • Forgetting .value with computed properties. full is a computed: in the script you read full.value, just like a ref.
  • Thinking the Options API is deprecated. It is not: you can keep using and maintaining it. It is simply not the recommended style for new code.
  • Tip: when you read an example in the official documentation, check the API preference toggle (Options/Composition) in the sidebar to see the style you are interested in.

Exercises

Exercise 1. Add the favourites feature to the <script setup> version of RoomCard: an isFavorite data item and a button that toggles it. Display the text "Favourite: yes" or "Favourite: no" in the template. Keep the code grouped by feature.

Exercise 2. Translate the following <script setup> snippet into the Options API:

import { ref, computed, onMounted } from 'vue'

const hourlyPrice = 25
const hours = ref(2)
const total = computed(() => hourlyPrice * hours.value)

function addHour() {
  hours.value++
}

onMounted(() => {
  console.log('Quote calculator ready')
})

Exercise 3. A colleague writes this in an Options API component and the template always shows NaN. Find the mistake:

export default {
  data() {
    return { attendees: 0, capacity: 8 }
  },
  computed: {
    freeSeats: () => this.capacity - this.attendees
  }
}

Solutions

Solution 1.

// --- Favourites --- (add after the occupancy block)
const isFavorite = ref(false)

function toggleFavorite() {
  isFavorite.value = !isFavorite.value
}

In the template:

<p>Favourite: {{ isFavorite ? 'yes' : 'no' }}</p>
<button @click="toggleFavorite">Toggle favourite</button>

We use a ternary expression inside the interpolation; the expressions allowed in templates are explained in Template Syntax.

Solution 2.

export default {
  data() {
    return {
      hourlyPrice: 25,
      hours: 2
    }
  },
  computed: {
    total() {
      return this.hourlyPrice * this.hours
    }
  },
  methods: {
    addHour() {
      this.hours++
    }
  },
  mounted() {
    console.log('Quote calculator ready')
  }
}

Notice that in the Options API hourlyPrice ends up in data (and is therefore reactive even though it never changes). It could also be declared as a constant outside the exported object.

Solution 3. The computed property is written as an arrow function. Arrow functions do not have their own this, so this.capacity and this.attendees are undefined and the subtraction gives NaN. It must be written as a regular method:

computed: {
  freeSeats() {
    return this.capacity - this.attendees
  }
}

This mistake is specific to the Options API: in the Composition API there is no this, so the problem disappears.

Conclusion

Vue offers two API styles for writing components. The Options API organises code by option type (data, computed, methods, watch, hooks) and accesses everything through this; the Composition API uses functions imported from vue (ref, computed, watch, onMounted) inside setup() or, more concisely, in <script setup>, which lets you group code by feature, reuse it through composables and get better type inference. You have written the draft of RoomCard in all three variants and you have an equivalence table for translating between them.

This brings the introductory module to a close: Colmena Bookings now has its Vite project, a header with ReceptionClock, a free-rooms counter and a first draft of RoomCard written with <script setup>, the style we will use by default for the rest of the course. So far we have used {{ }} interpolation and @click intuitively; in the next module we will start by studying rigorously what can be expressed in a template, in the Template Syntax lesson.

New edition · October 2026

Vue.js Course

Module 1: Introduction to Vue.js

Module 2: Vue.js Basics

Module 3: Vue.js Components

Module 4: Vue Router

Module 5: State Management with Vuex and Pinia

Module 6: Vue.js Directives

Module 7: Vue.js Plugins

Module 8: Testing in Vue.js

Module 9: Advanced Vue.js Concepts

Module 10: Building and Deploying Vue.js Applications

Module 11: Real-World Vue.js Projects

© Copyright 2026. All rights reserved