Vue 3 introduced the Composition API, but many developers still debate Composition vs Options API Vue—is it worth the transition? In this article, we’ll provide a detailed comparison of the Composition API and Options API in Vue, covering essential factors like code organization, reusability, TypeScript support, performance, and developer experience. This guide aims to help Vue.js developers make informed decisions about their application architecture.
Master the Composition API, build real-world apps with the
<script setup>
syntax, and leverage TypeScript in the Vue.js Master Class 2024 Edition course on Vue School.
Vue.js has two primary ways of defining component logic:
These are referred to as "APIs" because they provide a standardized set of methods, properties, and conventions for interacting with the Vue.js framework. The Options API organizes component logic into a set of options like data
, methods
, and computed
, while the Composition API uses composable functions like usePointer
and useFetch
to achieve similar functionality in a more modular way.
Both APIs give developers access to Vue.js' core reactivity system and lifecycle hooks, allowing them to build dynamic, responsive user interfaces. Understanding the tradeoffs between these two APIs is an important part of becoming an effective Vue.js developer.
The Options API was the foundational API used in Vue 2 and remains prevalent even in Vue 3. This approach organizes component logic into clearly defined sections, primarily data
, methods
, computed
, and watch
. Each section serves its specific purpose, allowing for a predictable structure. Here is a typical example:
<template>
<div>
<h1>{{ title }}</h1>
<button @click="increment">Increment</button>
<p>Count: {{ count }}</p>
</div>
</template>
<script>
export default {
data() {
return {
title: 'Hello Vue!',
count: 0,
};
},
methods: {
increment() {
this.count++;
},
},
};
</script>
In this example, methods and reactive properties are grouped in logical sections. While this aids understanding for small components, it can become increasingly cumbersome as component complexity scales, leading to potential fragmentation in larger applications.
The Composition API offers a paradigm shift, empowering developers to compose component logic in a function-based manner. This new approach introduces flexibility in managing reactive states and visualizes code organization around logical functionality rather than strict lifecycle hooks.
<template>
<div>
<h1>{{ title }}</h1>
<button @click="increment">Increment</button>
<p>Count: {{ count }}</p>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
const title = ref('Hello Vue!');
const count = ref(0);
function increment() {
count.value++;
}
</script>
In this snippet, the use of ref()
allows for reactive state declaration in a more concise manner. This function-based approach enhances clarity, making it easier to group related functionalities, leading to a more monolithic view of component logic.
A significant factor influencing the choice between these APIs is how they handle code organization. The Options API can lead to a disjointed experience as it separates component logic into distinct sections. This can create challenges, particularly when managing related state and behavior.
As a Vue.js component grows in size or complexity, developers often find themselves dealing with scattered pieces of logic. For instance, when updating multiple reactive states or related computations, the disjoint structure of the Options API can make it difficult to maintain clarity and cohesion.
The Composition API encourages developers to encapsulate related logic in a coherent manner. By using composition functions, developers can group data and methods that interact with one another. This not only improves readability but also provides a clearer mental model of how different parts of the component interact.
Consider a use case involving a form that requires validation logic alongside state management:
Options API Version:
<template>
<form @submit.prevent="submit">
<input v-model="name" placeholder="Enter your name" />
<input v-model="email" type="email" placeholder="Enter your email" />
<button :disabled="!isValid">Submit</button>
</form>
</template>
<script>
export default {
data() {
return {
name: '',
email: '',
};
},
computed: {
isValid() {
return this.name && this.email.includes('@');
},
},
methods: {
submit() {
if (this.isValid) {
alert(`Submitting: ${this.name}, ${this.email}`);
}
},
},
};
</script>
In this Options API example, the logic for validation and submission is scattered across different parts of the component, potentially obscuring the actual flow of data.
Composition API Version:
<template>
<form @submit.prevent="submit">
<input v-model="name" placeholder="Enter your name" />
<input v-model="email" type="email" placeholder="Enter your email" />
<button :disabled="!isValid">Submit</button>
</form>
</template>
<script setup lang="ts">
import { ref, computed } from 'vue';
const name = ref('');
const email = ref('');
const isValid = computed(() => name.value && email.value.includes('@'));
function submit() {
if (isValid.value) {
alert(`Submitting: ${name.value}, ${email.value}`);
}
}
</script>
In the Composition API example, state variables and related logic are encapsulated together, improving maintainability and readability.
Code reusability is essential for maintaining scalable applications. The Options API offers mixins and higher-order components to facilitate code sharing, but these approaches can introduce hidden dependencies, leading to maintenance challenges. While in Composition API, it offers composables, which are more easy to maintain and reuse.
Mixins allow developers to define reusable pieces of logic that can be injected into multiple components. However, this can lead to unclear dependencies and behaviors because it is not always evident which mixin provides which functionality.
// mixin.js
export const formMixin = {
data() {
return {
name: '',
};
},
methods: {
resetForm() {
this.name = '';
},
},
};
// Component.vue
<script>
import { formMixin } from './mixin';
export default {
mixins: [formMixin],
};
</script>
The use of mixins can generate conflicts, especially when multiple mixins modify the same piece of state or introduce overlapping methods.
Mixins are supported in Vue 3 mainly for backwards compatibility, due to their widespread use in ecosystem libraries. Use of mixins, especially global mixins, should be avoided in application code.
The Composition API provides a clear and efficient way to create reusable functionality through composition functions or composables. These functions can encapsulate logic and its relevant reactive state that can be easily imported and shared across components, enhancing modularity and predictability.
Example: Custom Composable
// composables/useForm.js
import { ref } from 'vue';
export function useForm() {
const name = ref('');
const email = ref('');
const isValid = computed(() => name.value.trim() && email.value.includes('@'));
const resetForm = () => {
name.value = '';
email.value = '';
};
return { name, email, isValid, resetForm };
}
// Component.vue
<script setup lang="ts">
import { useForm } from './useForm';
const { name, email, isValid, resetForm } = useForm();
</script>
In this example, the useForm
composition function gracefully encapsulates the related form logic, making it apparent where specific functionalities reside and enhancing code maintainability.
In modern web development, TypeScript has become a standard for ensuring type safety. However, using TypeScript with the Options API can sometimes lead to ambiguous type inference, complicating the development process.
While you can implement TypeScript in the Options API, achieving clear type definitions often requires verbose annotations. This manual approach can frustrate developers, especially in larger codebases.
For example, to enable type inference for props in the Options API, wrap the component with defineComponent()
. This allows Vue to infer prop types based on the props
option, considering details like required: true
and default
.
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
// type inference enabled
props: {
name: String,
email: { type: String, required: true },
},
})
</script>
However, runtime props options only support constructor functions for simple types, and can't specify complex types like objects with nested properties or function signatures. To annotate complex prop types, use the PropType
utility type, as shown below:
import { defineComponent } from 'vue'
import type { PropType } from 'vue'
interface Book {
title: string
author: string
year: number
}
export default defineComponent({
props: {
book: {
type: Object as PropType<Book>,
required: true
},
callback: Function as PropType<(id: number) => void>
},
mounted() {
// Accessing properties
this.book.title // string
this.book.year // number
// TS Error: argument of type 'string' is not
// assignable to parameter of type 'number'
this.callback('123')
}
})
The Composition API is inherently more compatible with TypeScript, facilitating more robust type definitions and clearer inference. We simply use the defineProps
macro with pure types via a generic type argument to define our props and their type definitions.
<script setup lang="ts">
interface Book {
title: string
author: string
year: number
}
const props = defineProps<{
book: Book
callback: (id: number) => void
}>()
const { book, callback } = props
// Accessing properties
console.log(book.title) // string
console.log(book.year) // number
// TS Error: argument of type 'string' is not
// assignable to parameter of type 'number'
callback('123')
</script>
This level of clarity aids developers throughout the development lifecycle, reducing runtime errors and inconsistencies while boosting overall productivity.
As of Vue.js 3.5, we can use Reactive Props Destructure feature to define type-safe default values for our props:
interface Book {
title: string
author: string
year: number
}
interface Props {
book: Book
}
const {
book = { title: 'Lord of the Rings', author: 'J.R.R. Tolkien', year: 1954 }
} = defineProps<Props>()
It’s also worth mentioning that the Vue.js official docs is recommending using the Composition API with TypeScript support:
While Vue does support TypeScript usage with Options API, it is recommended to use Vue with TypeScript via Composition API as it offers simpler, more efficient and more robust type inference.
Performance optimization is crucial for delivering a smooth user experience. The structure of components directly affects application performance, particularly concerning bundle size and execution speed.
The Options API, while straightforward, can introduce performance overhead due to its structural rigidity. In larger components, where numerous data properties and methods are declared, Vue’s reactivity system may allocate unnecessary memory and manage redundant lifecycle hooks, potentially increasing load times and impacting responsiveness. The reliance on this
for property access also creates an instance proxy layer, which can hinder minification efficiency and add to the overall bundle size.
The Composition API offers a streamlined approach to component architecture, emphasizing logical feature grouping and granular imports. This structure not only enhances reactivity but also minimizes memory usage by eliminating the instance proxy overhead. Code written with the Composition API in <script setup>
allows for in-scope variable access, bypassing the proxy and enabling direct, optimized access to reactive properties. This inline, compiled function approach results in smaller, more efficient bundles and improves minification since variable names can be safely shortened across the code.
Example of Tree-shaking:
// Assuming a component utilizing only specific functions from a larger library
import { ref } from 'vue';
const title = ref('Hello World!'); // Importing only necessary parts
By leveraging granular imports, the Composition API supports efficient tree-shaking, reducing the risk of retaining extraneous code that can persist with the Options API. The result is a cleaner, more performant application architecture aligned with modern JavaScript best practices for advanced Vue.js applications.
Quoting from the Vue.js official docs about the Composition API performance gains:
Code written in Composition API and
<script setup>
is also more efficient and minification-friendly than Options API equivalent.
For senior developers focused on fine-tuning their applications, the Composition API’s optimizations are essential for enhancing both load times and runtime performance, making it the clear choice for sophisticated, high-performance projects.
A positive developer experience (DX) is a key factor in fostering productive development environments. The structure and organization of code play a significant role in ensuring developers can work efficiently without unnecessary confusion.
While the Options API is easy for beginners to grasp, as applications become more complex, the disjointed organization can slow development. Context-switching between different options (such as methods, data, and computed properties) may disrupt the flow of development.
The Composition API enhances the developer experience (DX) by allowing a more unified, feature-driven code structure. Unlike the Options API, which segments data, methods, and lifecycle hooks, the Composition API enables developers to organize related logic in one place, streamlining both readability and maintenance. This consolidated structure reduces cognitive load, making it easier for developers to grasp component functionality at a glance.
Example: Contextual Understanding
Here’s how a countdown timer can be structured in each API, showcasing the improved clarity and context in the Composition API approach:
Using Options API:
export default defineComponent({
data() {
return {
timeLeft: 60,
interval: null as NodeJS.Timeout | null
}
},
created() {
this.startTimer()
},
methods: {
startTimer() {
this.interval = setInterval(() => {
this.timeLeft--
if (this.timeLeft === 0 && this.interval) clearInterval(this.interval)
}, 1000)
}
},
beforeUnmount() {
if (this.interval) clearInterval(this.interval)
}
})
Using Composition API:
<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
const timeLeft = ref(60)
const interval = ref<NodeJS.Timeout | null>(null)
onMounted(() => {
interval.value = setInterval(() => {
timeLeft.value--
if (timeLeft.value === 0 && interval.value) clearInterval(interval.value)
}, 1000)
})
onBeforeUnmount(() => {
if (interval.value) clearInterval(interval.value)
})
</script>
In the Composition API example, all code related to the timer is grouped in the same logical context, creating a cohesive and easily understandable structure. By co-locating the timeLeft
state, interval setup, and cleanup logic, developers can quickly follow the flow of the timer without jumping between sections. The result is a cleaner, more intuitive component design that supports advanced development and readability.
The growing community around Vue.js and the rich ecosystem behind it are significant factors to consider when deciding between APIs. The Composition API has garnered increased attention, leading to the development of libraries and plugins that fully embrace its advantages.
As developers adopt the Composition API, various Vue libraries and tools are transitioning to support this approach. Solutions like Vue Router and Pinia now offer features that leverage the strengths of the Composition API, allowing developers to create cleaner, more efficient applications.
Example with Vue Router:
Developers can utilize the useRoute
and useRouter
composition functions seamlessly integrated into applications, encouraging cohesive and functional routing strategies.
<script setup lang="ts">
import { useRoute } from 'vue-router'
const { slug } = useRoute().params
const getArticle = async () =>
await ofetch(`https://example.com/api/articles/${slug}`)
</script>
One major advantage of the Composition API is access to VueUse. It’s like a treasure trove of powerful, ready-made composables. VueUse speeds up development and saves the time and complexity of building custom reusable composables from scratch. Check out the VueUse for Everyone course on Vue School for get the best out of it.
As a final takeaway, here’s a quick guide to help you decide when to use Composition API or Options API based on project needs and complexity:
The Options API still holds value in specific scenarios:
The Composition API shines in various contexts:
For projects already built with Options API, incorporating Composition API via the setup function selectively can ease the transition to a more flexible structure. This approach is useful when introducing new libraries or functionalities that benefit from Composition API’s reusability and TypeScript integration.
The transition from the Options API to the Composition API presents both opportunities and challenges for developers in the Vue.js ecosystem. While the Options API has its strengths, especially in simpler contexts, the Composition API provides a more flexible and scalable approach for modern applications, particularly those characterized by increased complexity.
Ultimately, the choice between the two APIs should be informed by the specific needs of the project, team familiarity, and long-term maintainability goals.
Interested to learn more about Vue.js and Composition API? Boost your Vue.js skills with the Vue.js Master Class 2024 Edition, where we dive into Vue 3, the Composition API, <script setup>
, and TypeScript.
Our goal is to be the number one source of Vue.js knowledge for all skill levels. We offer the knowledge of our industry leaders through awesome video courses for a ridiculously low price.
More than 200.000 users have already joined us. You are welcome too!
© All rights reserved. Made with ❤️ by BitterBrains, Inc.