Vue 3 Computed Properties
Computed properties are used to dynamically compute derived values based on changes in other data, and they have a caching mechanism—they only recompute when their relevant dependencies change.
Computed property keyword: computed.
Computed properties are very useful when handling complex logic.
You can look at the following example of reversing a string:
Example 1
<div id="app">{{ message.split('').reverse().join('') }}
div
Try it »
In Example 1 the template becomes very complex and hard to read and understand.
Next, let's look at an example that uses computed properties:
Example 2
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<title>Vue Test Example - Runoob Tutorial (runoob.com)
title
<script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js" script head>
<body>
<div id="app">
<p>Original string: {{ message }}
p
<
p
Reversed string: {{ reversedMessage }}
p
div
<
script
const app = { data() { return { message: 'RUNOOB!!' } }, computed: { // computed property getter reversedMessage: function () { // this // points to the vm instance return this.message.split('').reverse().join('') } } } Vue.createApp(app).mount('#app')
script
body
html
Try it »
Example 2 declares a computed property reversedMessage.
The provided function is used as the getter for the property vm.reversedMessage.
vm.reversedMessage depends on vm.message; when vm.message changes, vm.reversedMessage also updates.
Using the computed function to define computed properties:
Example
<
template
<
div
<
pProduct name:
{
{
productName
}
}
p
<
p
Product price:
{
{
formattedPrice
}
}
p
<
button
@
click
=
"increasePrice"Increase price
button
div
template
<script import{reactive,computed}from'vue'>;
export
default
{
setup
(
)
{
// reactive data
const
state
=
reactive
(
{
name
:
'phone'
,
price
:
2000
}
)
;
// computed property
const
productName
=
computed
(
(
)
=>
{
return
`discount $
{
state.
name
}
`
;
}
)
;
const
formattedPrice
=
computed
(
(
)
=>
{
return
`¥$
{
state.
price
.
toFixed
(
2
)
}
`
;
}
)
;
// method
const
increasePrice
=
(
)
=>
{
state.
price
+=
100
;
}
;
return
{
productName
,
formattedPrice
,
increasePrice
}
;
}
}
;
script
Description
computed function:
- in
setupused in the functioncomputedfunction to define computed properties. - The arrow function returns the computed value and automatically tracks its dependent reactive data (
statein the objectnameandprice).
Using computed properties:
productNameComputed properties are derived fromstate.name, and wheneverstate.namechanges,productNameupdates automatically.formattedPriceComputed properties are derived fromstate.price, and wheneverstate.pricechanges,formattedPriceupdates automatically.
Reactive Data:
- Use
reactivefunction createsstateobject, making it reactive data whose property changes can be observed.
Method:
increasePriceThe method is used to increasestate.price; each button clickstate.priceincreases by 100, andformattedPricethe computed property updates accordingly.
computed vs methods
We can use methods instead of computed; the effect is the same in both cases, but computed is based on dependency caching and only re-evaluates when its related dependencies change. With methods, the function is always re-invoked on re-render.
Example 3
methods
:
{
reversedMessage2
:
function
(
)
{
return
this
.
message
.
split
(
'
'
)
.
reverse
(
)
.
join
(
'
'
)
}
}
Try it »
It can be said that using computed has better performance, but if you don't want caching, you can use the methods property.
computed setter
A computed property has only a getter by default, but you can also provide a setter when needed:
Example 4
const
app
=
{
data
(
)
{
return
{
name
:
'
'
,
url
:
'
http://www.google.com
'
}
}
,
computed
:
{
site
:
{
//
getter
get
:
function
(
)
{
return
this
.
name
'
'
this
.
url
}
,
//
setter
set
:
function
(
newValue
)
{
var
names
=
newValue
.
split
(
'
'
)
this
.
name
=
names
[
0
]
this
.
url
=
names
[
names
.
length
1
]
}
}
}
}
vm
=
Vue
.
createApp
(
app
)
.
mount
(
'
#app
'
)
document
.
write
(
'
name:
'
vm
.
name
)
;
document
.
write
(
'
'
)
;
document
.
write
(
'
url:
'
vm
.
url
)
;
document
.
write
(
'
------ Update data ------
'
)
;
//
Calling the setter, vm.name and vm.url are also updated accordingly
vm
.
site
=
'
Runoob Tutorial https://www.runoob.com
'
;
document
.
write
(
'
name:
'
vm
.
name
)
;
document
.
write
(
'
'
)
;
document
.
write
(
'
url:
'
vm
.
url
)
;
Try it »
From the example's runtime result, when running vm.site = 'Runoob Tutorial http://www.runoob.com';,setter is called, vm.name and vm.url is also updated accordingly.
