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.0, you can perform reactive computation and listening through several features, primarily ref
, reactive
, and computed
. Here's a tutorial showcasing how you can use these features:
Step 1: Initialize your Vue project.
Create a new Vue 3 project using Vue CLI.
vue create my-project
Choose Vue 3 when asked which version of Vue to use.
Step 2: In your component, import ref
, reactive
, and computed
from vue
.
<template> <div> <input v-model="person.name"> <p>Full name: {{ fullName }}</p> </div> </template> <script> import { ref, reactive, computed } from 'vue'; export default { setup() { const person = reactive({ name: 'John Doe', lastName: 'Smith' }); const fullName = computed(() => `${person.name} ${person.lastName}`); return { person, fullName }; }, }; </script>
In this example, we use the reactive
function to create a reactive object called person
. person
has two properties: name
and lastName
. We then use the computed
function to create a computed property fullName
. fullName
is a function that returns the full name of the person, which is the concatenation of person.name
and person.lastName
. This computed property is reactive, which means it will automatically update whenever person.name
or person.lastName
changes.
The setup
method is a new component option in Vue 3.0 that serves as the entry point for using the Composition API within components.
Step 3: Run your application.
You can run your Vue 3 application with the following command:
npm run serve
You should see an input box pre-populated with 'John Doe'. As you change the name in the input box, you will see the full name also updates automatically, demonstrating the reactive computations and listening.
The ref
function can be used similarly to create a reactive reference. The main difference is that ref
is used for primitive data types (like string, number) whereas reactive
is used for objects.
Vue 3.0 watchEffect
Function:
watchEffect
is a function that automatically tracks reactive dependencies and executes its callback when any of those dependencies change.<script> import { ref, watchEffect } from 'vue'; export default { setup() { const count = ref(0); watchEffect(() => { console.log(`Count changed: ${count.value}`); }); return { count, }; }, }; </script>
Vue 3.0 watch
Function:
watch
is a function that allows for more fine-grained control over reactive dependency tracking and offers before and after hooks.watch( () => count.value, (newValue, oldValue) => { console.log(`Count changed from ${oldValue} to ${newValue}`); } );
Vue 3.0 Watching Multiple Properties:
watch([() => prop1, () => prop2], ([newProp1, newProp2], [oldProp1, oldProp2]) => { // Handle changes });
Reactive Data Binding in Vue 3.0 Templates:
ref
or accessed through the value
property.<template> <div>{{ count }}</div> </template>
How to Use ref
in Vue 3.0:
ref
is used to create a reactive reference to a value.const count = ref(0);