Vue3 Computed Property
Computed The property is the way to declare computed properties that depend on other data in Vue.js.
Computed It allows you to encapsulate complex logic within a property and use it in templates just like ordinary data.
Computed The property's value updates automatically based on its dependent data, so when the data changes,computed the property is also recalculated automatically.
Why Use the Computed Property?
-
Simplify Template Logic
In Vue, writing complex logic directly in templates can make the code hard to maintain. By using
computed property, you can move complex logic out of templates, making the code clearer.
2.
Performance optimization
Computed The property is cached based on its dependent data. It is only recalculated when the dependent data changes,computed which avoids unnecessary computations and improves performance.
Reactive Updates
Vue's reactivity system automatically tracks the computed property's dependencies and recalculates the value when they change, ensuring the data always stays up to date.
How to Use the Computed Property?
In Vue 3,computed The property can be defined via the computed function. Here is a simple example:
Example
import
{
ref
,
computed
}
from
'vue'
;
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 Analysis
reffunction
is used to create reactive data.firstNameandlastNameis a reactive variable.computedfunction
is used to define computed properties.fullNameis acomputedproperty that depends onfirstNameandlastName.- Using in Templates
In templates, you can directly usefullName, and it will automatically update based onfirstNameandlastNamevalue.
The Caching Mechanism of Computed Properties
Computed The property's value is cached and is only recalculated when its dependent data changes. For example:
Example
const
count
=
ref
(
0
)
;
const
doubleCount
=
computed
(
(
)
=>
count.
value
2
)
;
console.
log
(
doubleCount.
value
)
;
// Output: 0
count.
value
=
2
;
console.
log
(
doubleCount.
value
)
;
// Output: 4
In this example,doubleCount The value is only recalculated when count changes.
Computed Property vs Methods
Computed The property and methods can both be used to encapsulate logic, but they have the following differences:
- Caching
ComputedThe property caches results, whilemethodsis re-executed on every call. - Performance
If the logic is complex and called frequently, using thecomputedproperty can improve performance. - Use Cases
ComputedThe property is suitable for values derived from other data, whilemethodsis suitable for performing actions or handling events.
Example Comparison:
Example
const
count
=
ref
(
0
)
;
// Using computed property
const
doubleCount
=
computed
(
(
)
=>
count.
value
2
)
;
// Using methods
function
getDoubleCount
(
)
{
return
count.
value
2
;
}
Vue3 Options API