With the Composition API, Vue 3 no longer uses data(). Use ref() or reactive() in setup() to manage your component state.
Scalable, Secure, and High-Performance Solutions for Your Business.
In Vue 2, the data() option in the Options API returns an object with your component’s state, like a simple count value.
export default { data() {
return {
count: 0
}
}
}
In Vue 3’s Composition API, there’s no data() function. Instead, you define reactive state inside the setup() function using ref() or reactive().
<script setup>import { ref } from ‘vue’
const count = ref(0)
</script>
<template>
<button @click=”count++”>Clicked {{ count }} times</button>
</template>
<script setup>import { reactive } from ‘vue’
const state = reactive({
count: 0,
name: ‘Vue 3’
})
</script>
<template>
<button @click=”state.count++”>
Clicked {{ state.count }} times
</button>
<p>{{ state.name }}</p>
</template>
Also Read: Best Vue Component Libraries to Build Stunning Frontends
eSparkBiz is rated 4.9 Stars
Real People, Real Stories
See why 300+ startups & enterprises trust eSparkBiz with their software outsourcingIn Vue 2, you need to explicitly install plugins using Vue.use() to add to Vue’s capabilities. This method installs the plugin globally so all components…
In Vue 3, the best way to define global variables is by adding them to app.config.globalProperties during app setup. This makes them accessible across the…
In Vue 3's Composition API, watching for prop changes is essential when you need to respond to updated values passed from a parent component. Unlike…
Let’s discuss how our dedicated experts can help.