Vue Composition API
The Composition API is an alternative way of writing Vue applications to the Options API that is used elsewhere in this tutorial.
In Composition API we can write code more freely, but it requires a deeper understanding, and it is considered to be less beginner-friendly.
The Composition API
With Composition API, logic is written using imported Vue functions instead of using the Vue instance structure that we are used to from Options API.
This is how Composition API can be used to write a Vue application that decreases the number of typewriters in storage with a button:
Example
App.vue
:
<template>
<h1>Example</h1>
<img src="/img_typewriter.jpeg" alt="Typewriter">
<p>Typewriters left in storage: {{ typeWriters }}</p>
<button @click="remove">Remove one</button>
<p style="font-style: italic;">"{{ storageComment }}"</p>
</template>
<script setup>
import { ref, computed } from 'vue'
const typeWriters = ref(10);
function remove(){
if(typeWriters.value>0){
typeWriters.value--;
}
}
const storageComment = computed(
function(){
if(typeWriters.value > 5) {
return "Many left"
}
else if(typeWriters.value > 0){
return "Very few left"
}
else {
return "No typewriters left"
}
}
)
</script>
Run Example »
On line 9 in the example above, the setup
attribute makes it easier to use Composition API. For example, by using the setup
attribute, variables and functions can be used directly inside the <template>
.
On line 10, ref
and computed
must be imported before they can be used. In Options API, we do not need to import anything to declare reactive variables or to use computed properties.
On line 12, ref
is used to declare the 'typewriters' property as reactive with '10' as the initial value.
To declare the 'typewriters' property as reactive means that the line {{ typewriters }}
in the <template>
will be re-rendered automatically to show the updated value when the 'typewriters' property value is changed. With Option API, data properties become reactive if they need to be when the application is built, they do not need to be declared explicitly as reactive.
The 'remove()' function declared on line 14 would be declared under the Vue property 'methods' if the example was written in Options API.
The 'storageComment' computed property on line 20 would be declared under the Vue property 'computed' if the example was written in Options API.
The Options API
The Options API is what is used elsewhere in this tutorial.
The Options API is chosen for this tutorial because it has a recognizable structure and is considered easier to start with for beginners.
As an example, the structure in the Options API has the data properties, methods and computed properties all placed in different parts of the Vue instance, clearly separated.
Here is the example above written with Options API:
Example
App.vue
:
<template>
<h1>Example</h1>
<img src="/img_typewriter.jpeg" alt="Typewriter">
<p>Typewriters left in storage: {{ typeWriters }}</p>
<button @click="remove">Remove one</button>
<p style="font-style: italic;">"{{ storageComment }}"</p>
</template>
<script>
export default {
data() {
return {
typeWriters: 10
};
},
methods: {
remove(){
if(this.typeWriters>0){
this.typeWriters--;
}
}
},
computed: {
storageComment(){
if(this.typeWriters > 5) {
return "Many left"
}
else if(this.typeWriters > 0){
return "Very few left"
}
else {
return "No typewriters left"
}
}
}
}
</script>
Run Example »