How to use the new composition API in Vue

In this article we will be using Vue Composition API inside Vue2.x project setup and build a simple todo app

Ishan Manandhar
Nov 4 · 3 min read

Composition API is most commonly discussed and new feature syntax of the next major version release of Vue. It’s a completely new approach in making our logic reusable and organize our codebase.

How to get started with the API

We will be using the composition API inside our Vue2.x project with the help of existing plugin, first create our app with Vue CLI

vue create composition-todo

Then we need to install the plugin from our command line:

yarn add @vue/composition-api

We will initialize the plugin into our main.js file with code below:

import Vue from 'vue'
import App from './App.vue'
import CompositionApi from '@vue/composition-api'

Vue.config.productionTip = false
Vue.use(CompositionApi)

new Vue({
render: h => h(App),
}).$mount('#app')

This is all we need to move ahead working with the API. We will get into using reactive, ref, computed, Vue lifecycle and more. Let’s move on with the code by making our brand new component inside our app.vue file we will import reactive from our API and write the code below

<template>
<div id="app">
<section>
<input type="text" v-model="state.todo" />
<ul>
<li v-for="(todo, i) in state.todolist" :key="i">{{todo}}</li>
</ul>
</section>
<button @click="addTodo">Add Todo</button>
</div>
</template>
<script>
import { reactive } from "@vue/composition-api";
export default {
setup() {
const { state, addTodo } = useOurTodo();
return {
state,
addTodo
};
}
};
function useOurTodo() {
let state = reactive({
todo: "",
todolist: []
});
function addTodo() {
state.todolist.push(state.todo);
state.todo = "";
}
return {
state,
addTodo
};
}
</script>
<style lang="sass">
</style>

If we look into our code above we can see we have created a setup function and set our reactive data properties and returned its value. With the help of the API we are able to structure our function more readable and reusable way in contrast in the options API before it would have been forced according to structure. We can split our code into little modules.

Now we will further group the logic in different section. We will make our changes to make it more well structured.

<template>
<div id="app">
<section>
<h3>My Todo List</h3>
<input type="text" v-model="state.todo" />
<ul>
<li v-for="(todo, i) in state.todolist" :key="i">
{{todo}}
<button @click="removeTodo(i)">x</button>
</li>
</ul>
</section>
<button @click="addTodo">Add Todo</button>
</div>
</template>

<script>
import { reactive } from "@vue/composition-api";
export default {
setup() {
const { state, addTodo, removeTodo } = useOurTodo();
return {
state,
addTodo,
removeTodo
};
}
};

function useOurTodo() {
let state = reactive({
todo: "",
todolist: []
});

function addTodo() {
state.todolist.push(state.todo);
state.todo = "";
}
function removeTodo(i) {
state.todolist.splice(i);
}
return {
state,
addTodo,
removeTodo
};
}
</script>

<style lang="sass">
</style>

We have made our further changes to the code. This way we can get into more cleaner way in writing our code which can be useful in our code structuring and modularity. Similarly we can use more of the provided composition API features like Props, watchers, context, Lifecycle hooks which will be covered in next article writing.

The code now is improved, but it showcases the importance of logical separation. We don’t have to understand which data or properties or methods belong to which component area, we can simply group it in more readable, reusable, modular functions now.

You can get the code from Github, also please check branches=> shorturl.at/hxU34

************************ — Happy Coding — **********************

Reference readings:

JavaScript in Plain English

Learn the web's most important programming language.

Ishan Manandhar

Written by

Product Designer and Frontend Developer | ishan02016@gmail.com | http://ishan.surge.sh/

JavaScript in Plain English

Learn the web's most important programming language.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade