Vue.js 3.0 Tutorial
Vue.js 3.0 Component Advanced
Vue.js 3.0 Transitions & Animations
Vue.js 3.0 Reusable & Combinable
Vue.js 3.0 Advanced
Vue.js 3.0 Tools
Vue.js 3.0 Scale
Vue.js 3.0 Accessibility
Vue.js 3.0 Migrating from Vue2
Vue.js 3.0 Contribute Documentation
Vue.js 3.0 API References
Vue.js 3.0 Style Guide
In Vue 3, the way to define reactive data in your components hasn't changed significantly from Vue 2 if you're using the Options API. The data
option is a function that returns an object, and each property of that object is a reactive data source that Vue.js will track and update the DOM when changes are made.
Here's a step-by-step tutorial on how to use the data
option in Vue 3.0:
Step 1: Initialize your Vue project.
If you haven't created your Vue 3 project yet, you can do so using the Vue CLI.
vue create my-project
Choose Vue 3 when asked which version of Vue to use.
Step 2: In your App.vue
file (or any component where you want to use reactive data), define a data
function that returns an object.
Here's an example:
<template> <div class="app"> <h1>{{ message }}</h1> <button @click="changeMessage">Change Message</button> </div> </template> <script> export default { data() { return { message: 'Hello Vue 3!' }; }, methods: { changeMessage() { this.message = 'Message has been changed!'; } } }; </script>
In the above example, message
is a reactive data property. We display the message
in the template with the mustache syntax {{ message }}
. We also define a changeMessage
method which changes the value of message
when called. This method is attached to a button click event.
Step 3: Run your application.
You can run your Vue 3 application with the following command:
npm run serve
Now, when you click the "Change Message" button, the message displayed on the screen should change to "Message has been changed!". This is because Vue 3 is reactive, and it updates the DOM in response to changes in the data properties.
That's how you define and use reactive data in Vue 3.0 using the Options API! Remember, with the Options API, data should be a function that returns an object, and every property of that object is reactive.
Vue 3.0 Data Property in Component Options:
data
property in a Vue component to store reactive data.<!-- MyComponent.vue --> <script> export default { data() { return { message: 'Hello, Vue 3.0!', }; }, }; </script>