Skip to content

Lesson 9 of 9 · 27m

Migrating Options API Codebases to Vue 3 Composition API

A staged migration strategy for real codebases — compatibility boundaries, composable extraction, mixed-style operation, and how not to rewrite the company.

The worst Vue migration plan is a rewrite disguised as a migration. Production systems have checkout flows, admin hacks, forgotten mixins, and tests that only exist in the behavior of users. The right plan is incremental: stabilize the runtime, isolate feature boundaries, extract logic into composables, and migrate components when there is a business reason to touch them.

Inventory before converting

Classify components by risk and churn:

high churn + high business risk     -> migrate deliberately, with tests
high churn + low risk               -> good early candidates
low churn + high risk               -> leave alone unless necessary
low churn + low risk                -> cosmetic migration, low priority

Also inventory cross-cutting mechanisms: global mixins, custom directives, filters, event buses, Vuex modules, prototype properties (this.$http), and implicit dependency injection. Those are migration risks regardless of how clean individual components look.

Extract logic before rewriting templates

Options API lifecycle and computed logic often compress neatly into composables:

export function useInvoiceTotals(invoice: Ref<Invoice>) {
  const subtotal = computed(() =>
    invoice.value.lines.reduce((sum, line) => sum + line.qty * line.price, 0),
  );
  const tax = computed(() => subtotal.value * invoice.value.taxRate);
  const total = computed(() => subtotal.value + tax.value);
  return { subtotal, tax, total };
}

This extraction can be tested independently even while the component remains Options API. Then the component becomes thinner, and the eventual conversion is mechanical rather than interpretive.

Convert leaf components first, containers later

Leaf inputs, rows, badges, and panels have narrow contracts and low coupling. Containers own routes, stores, and orchestration. Migrating leaves first gives you practice without destabilizing application flow. Mixed Options and Composition API usage is acceptable during migration; Vue supports it. Perfectionism is what kills projects, not coexistence.

Replace event buses deliberately

Legacy Vue 2 code often uses an event bus because cross-component communication had few good options. In Vue 3, decide the replacement per use case: emits for parent-child interaction, provide/inject for scoped dependencies, Pinia for genuinely shared workflows, and router state for navigation. Do not recreate a global untyped bus and call it modern.

Migration mechanics that prevent pain

  • Run vue and @vue/compat diagnostics if migrating from Vue 2; fix warnings before disabling compatibility.
  • Upgrade tests around critical flows before changing implementation style.
  • Convert Vuex modules in bounded slices rather than trying to swap every module to Pinia in one PR.
  • Keep PRs component-sized or feature-sized. Giant migration branches merge conflict themselves into oblivion.

Field notes

  • Filters disappeared in Vue 3; move formatting into functions or computed values.
  • $listeners was removed; attributes and listeners now live together in $attrs.
  • this does not exist in <script setup>. If a migration relies on it heavily, extract logic instead of translating blindly.
  • A component that has not changed in three years does not need conversion merely to satisfy stylistic purity. Spend risk where it buys maintainability.

Was this lesson helpful?

Sign in to rate