Skip to content

Chapter 2 of 8

Component API Styles: Options vs Composition

Vue offers two main API styles for organizing component logic. The traditional Options API organizes code into predefined options such as data() for reactive state, methods for functions, computed for derived values, watch for side effects, and lifecycle hooks like mounted(). This approach is intuitive for newcomers because each concern has a designated place in the component definition, and it remains fully supported in Vue 3.

The Composition API, introduced and recommended in Vue 3, takes a different approach by exposing function-based APIs such as ref, reactive, computed, watch, and lifecycle hooks that can be composed freely. This style is used inside a setup() function or, more commonly, inside the script setup block. The advantage of the Composition API is that it lets you group related logic by feature rather than by option type, which makes complex components easier to read and reason about, and it pairs naturally with TypeScript. Both styles are fully supported, and you can even mix them within the same project depending on the needs of each component.

The script setup syntax is a compile-time syntactic sugar layered on top of the Composition API. Inside this block, every top-level binding—including variables, functions, and imports—is automatically exposed to the template without needing to be returned from a setup() function. This dramatically reduces boilerplate, especially for component props and emits which are declared through the compiler macros defineProps and defineEmits. As a result, script setup has become the default and most ergonomic way to write Vue 3 components.

All chapters
  1. 1Introduction to Vue and the Reactivity System
  2. 2Component API Styles: Options vs Composition
  3. 3Template Directives
  4. 4Computed Properties and Watchers
  5. 5Component Lifecycle, Props, and Emits
  6. 6Slots, Provide/Inject, and Advanced Features
  7. 7State Management with Pinia
  8. 8Routing with Vue Router

Drill it

Reading is not remembering. These come from the Vue Framework deck:

Q

What is Vue.js?

Vue.js is a progressive JavaScript framework for building user interfaces. It is designed to be incrementally adoptable and focuses on the view layer, making it...

Q

What is the Vue reactivity system?

The Vue reactivity system automatically tracks dependencies and updates the DOM when reactive state changes. In Vue 3, it uses Proxy objects (instead of Object....

Q

How do you create a reactive object in Vue 3?

Use reactive() from the Composition API:import { reactive } from 'vue'const state = reactive({ count: 0, name: 'Vue' })This returns a deeply reactive proxy of t...

Q

What is the difference between ref() and reactive()?

ref() wraps a single value (primitive or object) and requires .value to access it in script. reactive() wraps an object and provides direct property access.cons...