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, computed properties and watchers (or listeners) are used to work with reactive data.
Computed properties are values derived from reactive data, and watchers listen for changes to reactive data to run side effects.
Here's a tutorial that illustrates how to use both in Vue 3:
Step 1: Initialize your Vue project.
Create a new Vue 3 project using Vue CLI:
npm install -g @vue/cli vue create my-project
Choose Vue 3 when asked which version of Vue to use.
Step 2: In your App.vue
file, import ref
, computed
, and watch
from Vue.
Here's an example:
<template> <div> <input v-model="message"> <p>Original message: "{{ message }}"</p> <p>Reversed message: "{{ reversedMessage }}"</p> </div> </template> <script> import { ref, computed, watch } from 'vue'; export default { setup() { const message = ref('Hello Vue 3!'); const reversedMessage = computed(() => message.value.split('').reverse().join('')); watch(message, (newValue, oldValue) => { console.log(`Message changed from "${oldValue}" to "${newValue}"`); }); return { message, reversedMessage }; }, }; </script>
In this example, we're creating a reactive message
using ref
and a reversedMessage
computed property that returns the message
reversed.
We're also setting up a watcher on the message
ref that logs to the console every time message
changes.
Step 3: Run your application.
You can run your Vue 3 application with the following command:
npm run serve
Now, if you change the text in the input field, you should see the reversed text update automatically. Additionally, you will see the console log the changes to the message
whenever you modify the input.
Using Computed Properties in Vue 3.0 Templates:
Computed properties are declared in the computed
option of a component.
They are used to perform calculations based on reactive data.
Example code:
<template> <div>{{ computedProperty }}</div> </template> <script> export default { data() { return { dataProperty: 10, }; }, computed: { computedProperty() { return this.dataProperty * 2; }, }, }; </script>
Computed Properties and Reactivity:
Computed properties are reactive, meaning they automatically update when dependent data changes.
Vue tracks dependencies and updates the computed property accordingly.
Example code:
computed: { fullName() { return this.firstName + ' ' + this.lastName; }, },
Computed Properties vs. Methods:
Computed properties are cached based on their dependencies, only re-evaluating when dependencies change.
Methods are called whenever the template is re-rendered, regardless of dependencies.
Example code:
computed: { fullName() { return this.firstName + ' ' + this.lastName; }, }, methods: { getFullName() { return this.firstName + ' ' + this.lastName; }, },
Computed Properties and Data Binding in Vue 3.0:
Computed properties can be directly used in templates for data binding.
Changes in computed properties are automatically reflected in the template.
Example code:
<template> <div>{{ reversedMessage }}</div> </template> <script> export default { data() { return { message: 'Hello Vue!', }; }, computed: { reversedMessage() { return this.message.split('').reverse().join(''); }, }, }; </script>
Dynamic Computed Properties in Vue 3.0:
Computed properties can be dynamic, depending on the state of the component.
Example code:
computed: { dynamicProperty() { return this.isDynamic ? 'Dynamic Value' : 'Static Value'; }, },
Computed Properties Caching:
Computed properties are cached and only re-evaluated when their dependencies change.
This improves performance by avoiding unnecessary recalculations.
Example code:
computed: { cachedProperty() { // Only re-evaluated if dependent data changes return heavyCalculation(this.dependentData); }, },
Computed Properties and Watchers:
Computed properties can be watched for changes using the watch
option.
Watchers are useful when you need to perform asynchronous or complex actions on computed property changes.
Example code:
watch: { computedProperty(newValue, oldValue) { // Handle changes to computed property }, },
Vue 3.0 v-on Directive with Computed Properties:
Computed properties can be used in conjunction with event handlers using the v-on
directive.
Example code:
<button v-on:click="handleClick">{{ computedButtonText }}</button>
computed: { computedButtonText() { return this.buttonText.toUpperCase(); }, }, methods: { handleClick() { console.log('Button clicked!'); }, },
Computed Properties and Custom Events:
Computed properties can be involved in custom event handling.
Use computed properties to calculate values for custom events.
Example code:
<custom-component v-on:custom-event="handleCustomEvent"></custom-component>
computed: { computedEventData() { return /* calculate data */; }, }, methods: { handleCustomEvent() { this.$emit('custom-event', this.computedEventData); }, },
Computed Properties and Reactivity Updates in Vue 3.0:
Reactivity updates in Vue 3.0 are automatic when dependent data changes.
Computed properties ensure that the UI reflects the latest changes in the data.
Example code:
<template> <div>{{ reactiveData }}</div> </template> <script> export default { data() { return { reactiveData: 'Initial Value', }; }, methods: { updateData() { this.reactiveData = 'Updated Value'; }, }, }; </script>