AboutBlogContact
FrontendNovember 25, 2018 2 min read 22

Vue 3: Migration and the New Composition API (2018)

AunimedaAunimeda
📋 Table of Contents

Vue 3: Migration and the New Composition API

In 2018, the Vue community is buzzing with the first RFCs for Vue 3. While Vue 2's "Options API" (data, methods, computed, etc.) has served us well, we've started to hit the "organization wall" in larger components. Features are spread across multiple options, and logic reuse via mixins is fraught with naming collisions.

The answer is the Composition API. It's not a replacement for the Options API, but a new way to organize your component's logic.

Why Composition API?

In Vue 2, logic for a single feature is fragmented. With the Composition API, you can keep all the code for a specific feature in one place, even in a separate file.

Practical Example: A Reusable Mouse Tracker

Before the Composition API, we would use a mixin. In 2018, we can use a "composable" function.

// useMouse.js
import { ref, onMounted, onUnmounted } from 'vue';

export function useMouse() {
  const x = ref(0);
  const y = ref(0);

  function update(event) {
    x.value = event.pageX;
    y.value = event.pageY;
  }

  onMounted(() => window.addEventListener('mousemove', update));
  onUnmounted(() => window.removeEventListener('mousemove', update));

  return { x, y };
}

And in your component:

<template>
  <div>Mouse position: {{ x }}, {{ y }}</div>
</template>

<script>
import { useMouse } from './useMouse';

export default {
  setup() {
    const { x, y } = useMouse();
    return { x, y };
  }
}
</script>

The Power of setup()

The setup() function is the entry point for the Composition API. It runs before the component is created, once the props are resolved. This is where you define all your reactive state, computed properties, and lifecycle hooks.

What about TypeScript?

One of the biggest drivers for Vue 3 in 2018 is better TypeScript support. The Composition API is built from the ground up to work with TypeScript's type inference, making it much easier to write type-safe components than with the Options API.

Vue 3 is still in the works, but the movement toward composition-based architecture is clear. If you're building large Vue apps in 2018, you should start thinking about how to refactor your mixins into composables.

Read Also

Qwik: Resumability vs. Hydration (2022)aunimeda
Frontend

Qwik: Resumability vs. Hydration (2022)

Is the era of hydration over? In 2022, Misko Hevery's Qwik introduces 'resumability' to achieve instant-on web apps.

WebGL: High-Performance Data Visualization on the Web (2021)aunimeda
Frontend

WebGL: High-Performance Data Visualization on the Web (2021)

When SVG and Canvas 2D hit their limits, WebGL is the answer. In 2021, we are visualizing millions of data points with the GPU.

SWC and esbuild: The End of Slow Bundling (2021)aunimeda
Frontend

SWC and esbuild: The End of Slow Bundling (2021)

Babel and Webpack have served us well, but the performance ceiling of JavaScript is real. In 2021, Rust and Go are rebuilding our toolchains.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

Get Consultation All articles