I'm trying to figure out the need for the 'new' Vue Composition API.
Say we have the following component, from their basic example:
<template>
<button @click="increment">
Count is: {{ state.count }}, double is: {{ state.double }}
</button>
</template>
<script>
import { reactive, computed } from '@vue/composition-api'
export default {
setup () {
const state = reactive({
count: 0,
double: computed(() => state.count * 2),
})
function increment () {
state.count++
}
return {
state,
increment,
}
},
}
</script>
We could also write this 'composition style' using plain JavaScript, and make it available via the data
option:
<template>
<button @click="increment">
Count is: {{ state.count }}, double is: {{ state.double }}
</button>
</template>
<script>
export default {
data () {
const state = {
count: 0,
get double () {
return this.count * 2
},
}
function increment () {
state.count++
}
return {
state,
increment,
}
},
}
</script>
Is there any functional difference between these two options? What are the advantages of the Composition API?