Option API vs Composition API: Choosing the Right Approach for Your Team Ben Hong @bencodezen
A presentation at Vue London 2021 in October 2021 in by Ben Hong
Option API vs Composition API: Choosing the Right Approach for Your Team Ben Hong @bencodezen
Sta DX Engineer at Netlify Vue.js Core Team Member A little about me Nuxt Ambassador Vue Mastery Instructor Google Developer Expert ff Options API vs Composition API @bencodezen Vue.js London 2021
Before Vue 3, things were “simpler”… Options API vs Composition API @bencodezen Vue.js London 2021
Because there was only one way to write your components: Options API Options API vs Composition API @bencodezen Vue.js London 2021
<template> <h1>Standard Counter</h1> <p>Current Count: {{ count }}</p> <p>Double Count: {{ doubleCount }}</p> <button @click=”incrementCount”>+</button> Options API vs Composition API @bencodezen Vue.js London 2021 </template>
But with Vue 3 introducing the Composition API, it got a little more complicated… Options API vs Composition API @bencodezen Vue.js London 2021
<template> <h1>Standard Counter</h1> <p>Current Count: {{ count }}</p> <p>Double Count: {{ doubleCount }}</p> <button @click=”incrementCount”>+</button> Options API vs Composition API @bencodezen Vue.js London 2021 </template>
<template> <h1>Standard Counter</h1> Options API vs Composition API @bencodezen Vue.js London 2021 <p>Current Count: {{ count }}</p>
<template> <h1>Standard Counter</h1> <p>Current Count: {{ count }}</p> <p>Double Count: {{ doubleCount }}</p> <button @click=”incrementCount”>+</button> Options API vs Composition API @bencodezen Vue.js London 2021 </template>
<template> <h1>Standard Counter</h1> <p>Current Count: {{ @bencodezen count }}</p> Vue.js London 2021 Options API vs Composition API <p>Double Count: {{ doubleCount }}</p>
Options API vs Composition API @bencodezen Vue.js London 2021
Options API vs Composition API @bencodezen Vue.js London 2021
Options API vs Composition API @bencodezen Vue.js London 2021
<template> <h1>Standard Counter</h1> <p>Current Count: {{ count }}</p> <p>Double Count: {{ doubleCount }}</p> <button @click=”incrementCount”>+</button> Options API vs Composition API @bencodezen Vue.js London 2021 </template>
And with Vue 3.2’s release of the new script setup syntax, it adds yet another style of writing components… Options API vs Composition API @bencodezen Vue.js London 2021
<template> <h1>Standard Counter</h1> <p>Current Count: {{ count }}</p> <p>Double Count: {{ doubleCount }}</p> <button @click=”incrementCount”>+</button> Options API vs Composition API @bencodezen Vue.js London 2021 </template>
Options API vs Composition API @bencodezen Vue.js London 2021
Options API vs Composition API @bencodezen Vue.js London 2021
Options API vs Composition API @bencodezen Vue.js London 2021
So which one is the “right” one? Options API vs Composition API @bencodezen Vue.js London 2021
Approach #1: Options API Options API vs Composition API @bencodezen Vue.js London 2021
<template> <h1>Standard Counter</h1> <p>Current Count: {{ count }}</p> <p>Double Count: {{ doubleCount }}</p> <button @click=”incrementCount”>+</button> Options API vs Composition API @bencodezen Vue.js London 2021 </template>
Approach #1: Options API Pros: • Easy to learn • Consistency • Simplicity Options API vs Composition API @bencodezen Vue.js London 2021
Approach #1: Options API Pros: • Easy to learn • Consistency • Simplicity Options API vs Composition API @bencodezen Cons: • Opinionated • Less flexibility • Not as TS friendly Vue.js London 2021
Approach #2: Composition API Options API vs Composition API @bencodezen Vue.js London 2021
Options API vs Composition API @bencodezen Vue.js London 2021
Approach #2: Composition API Pros: • Flexibility • “Just JavaScript” • TS friendly Options API vs Composition API @bencodezen Vue.js London 2021
Approach #2: Composition API Pros: • Flexibility • “Just JavaScript” • TS friendly Options API vs Composition API @bencodezen Cons: • More flexibility • Higher barrier of entry • Lack of structure Vue.js London 2021
Approach #3: Options API + Composition API Options API vs Composition API @bencodezen Vue.js London 2021
<template> <h1>Standard Counter</h1> <p>Current Count: {{ count }}</p> <p>Double Count: {{ doubleCount }}</p> <button @click=”incrementCount”>+</button> Options API vs Composition API @bencodezen Vue.js London 2021 </template>
Approach #3: Options API + Composition API Pros: • Structure w/ flexibility • Progressive enhancement • More TS friendly Options API vs Composition API @bencodezen Vue.js London 2021
Approach #3: Options API + Composition API Pros: • Structure w/ flexibility • Progressive enhancement • More TS friendly Options API vs Composition API @bencodezen Vue.js London 2021 Cons: • Two approaches • More verbose • Not as TS friendly
So which one is the “right” one? Options API vs Composition API @bencodezen Vue.js London 2021
What matters is choosing the right approach for your team. Options API vs Composition API @bencodezen Vue.js London 2021
Aspects to Consider Options API vs Composition API @bencodezen Vue.js London 2021
Aspects to Consider • Migrating or new? • Who is contributing? • Plan on using TypeScript heavily? • What does the team prefer? Options API vs Composition API @bencodezen Vue.js London 2021
⚖ Final Verdict? Options API vs Composition API @bencodezen Vue.js London 2021
Every approach is a valid one. Options API vs Composition API @bencodezen Vue.js London 2021
Every approach is a valid one regardless of what everyone else says. Options API vs Composition API @bencodezen Vue.js London 2021
Because at the end of the day, what matters are the features being shipped and whether the team enjoys working with the code base. Options API vs Composition API @bencodezen Vue.js London 2021
Thank you! bencodezen.io twitter.com/bencodezen youtube.com/bencodezen twitch.tv/bencodezen Options API vs Composition API @bencodezen Vue.js London 2021