Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Computed Properties

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

<

p

Product 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:

Using computed properties:

Reactive Data:

Method:


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

:

'

Google

'

,

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.