Vue3 Computed property
ComputedComputed properties are a way in Vue.js to declare calculated values that depend on other data.
ComputedIt allows you to encapsulate complex logic in a property and use it in templates like ordinary data.
ComputedThe value of the property automatically updates based on its dependent data, so when data changes,computedthe property will also be recalculated automatically.
Why use Computed properties?
Simplify template logic
In Vue, writing complex logic directly in templates can make code difficult to maintain. By usingcomputedcomputed properties, you can move complex logic out of templates, making the code clearer.Performance optimization
ComputedComputed properties are cached based on their dependent data. Only when the dependent data changescomputedwill the property be recalculated. This mechanism avoids unnecessary calculations and improves performance.Reactive updates
Vue's reactive system automatically trackscomputedthe dependencies of computed properties and recalculates values when dependencies change, ensuring data is always up to date.
How to use Computed properties?
In Vue3,computedcomputed properties can becomputeddefined using the function. Here is a simple example:
Example
export default {
setup() {
// Define a reactive data
const firstName = ref('John');
const lastName = ref('Doe');
// Define a computed property
const fullName = computed(() => {
return `${firstName.value} ${lastName.value}`;
});
return {
firstName,
lastName,
fullName,
};
},
};
Code explanation
refThe function
is used to create reactive data.firstNameandlastNameis a reactive variable.computedThe function
is used to define a computed property.fullNameis acomputedcomputed property, which depends onfirstNameandlastName。- Using in templates
In the template, you can directly usefullName, it will automatically update based onfirstNameandlastNamethe value of ...
Computed property caching mechanism
ComputedThe value of a computed property is cached, and it is recalculated only when its dependent data changes. For example:
Example
const doubleCount = computed(() => count.value * 2);
console.log(doubleCount.value); // Outputs 0
count.value = 2;
console.log(doubleCount.value); // Outputs 4
In this example,doubleCountthe value of ... is recalculated only whencountthe ... changes.
Computed property vs Methods
ComputedComputed properties andmethodsMethods can both be used to encapsulate logic, but they have the following differences:
- Caching
ComputedComputed properties cache results, whilemethodsMethods are re-executed on every call. - Performance
If the logic is complex and needs frequent calls, usingcomputedcomputed properties can improve performance. - Use cases
ComputedComputed properties are suitable for values calculated based on other data, whilemethodsmethods are suitable for performing operations or handling events.
Example comparison:
Example
// Use a computed property
const doubleCount = computed(() => count.value * 2);
// Use methods
function getDoubleCount() {
return count.value * 2;
}
Other extensions
Vue3 Options API