Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
API Styles  ›  Lesson

The Composition API and script setup

Composition API 13 minThe Composition API builds component logic from imported functions such as ref and computed, and script setup exposes it to the template.

The Composition API and script setup

In the Options API, the code for one feature is split across drawers: the reading counter's state in data, its percentage in computed, its button logic in methods. Add a second feature and the drawers mix two stories. The Composition API lets you keep each feature's code together, as plain JavaScript.

The rule

Import the tools, write plain variables and functions, no this.

The same progress counter, in the Composition API with <script setup>:

<script setup>
import { ref, computed, onMounted } from 'vue'

const props = defineProps({ pages: Number })

const pagesRead = ref(0)
const percent = computed(() => Math.round(pagesRead.value / props.pages * 100))

function addPage() {
  pagesRead.value++
}

onMounted(() => console.log('Progress counter is on the page'))
</script>

<template>
  <p>{{ pagesRead }} / {{ pages }} ({{ percent }}%)</p>
  <button @click="addPage">+1 page</button>
</template>

The drawers turned into imported functions:

Options API Composition API
data() ref() or reactive()
computed: {} computed(() => ...)
methods: {} ordinary functions
mounted() onMounted(() => ...)
props: {} defineProps()

<script setup> is the short form. It runs once when each instance is created, and every top-level binding is handed to the template. Without it you would write export default { setup() { ...; return { pagesRead, addPage } } } and list everything by hand.

Because features are plain code, you can lift one out into a function in its own file, often named useSomething, and reuse it in many components:

// useProgress.js
import { ref, computed } from 'vue'
export function useProgress(pages) {
  const pagesRead = ref(0)
  const percent = computed(() => Math.round(pagesRead.value / pages * 100))
  return { pagesRead, percent }
}

Such functions are called composables. Both API styles are fully supported in Vue 3; the Composition API is the recommended choice for new projects.

The shortcut

Translating Options to Composition is mechanical: drop this, add .value. Each this.x becomes x.value if x is a ref, or props.x if it is a prop.

Translating `addPage`Step 1 of 4
  1. Old code

    addPage() { this.pagesRead++ } in methods.

Drills

Answer fast — trust the rule.

quick drill

In the Composition API, what replaces the data() option?

quick drill

What does <script setup> save you from writing?

quick drill

Translate this.pagesRead = 0 (where pagesRead is state) to the Composition API.

quick drill

What is a composable?

Remember

  • The Composition API builds logic from imported functions: ref, computed, onMounted.
  • Shortcut: drop this, add .value.
  • Trap: reaching for this inside <script setup>; it does not refer to the component there.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTranslate a component

Rewrite this Options API piece in <script setup>: data() { return { pagesRead: 0 } }, methods: { addPage() { this.pagesRead++ } }. Explain what replaced each option.