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

Vue3 computed Property

Vue3 Computed Property


Vue3 Options API Vue3 Options API

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?

  1. 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.

3.

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

  1. ref function
    is used to create reactive data.firstName and lastName is a reactive variable.
  2. computed function
    is used to define computed properties.fullName is a computed property that depends on firstName and lastName.
  3. Using in Templates
    In templates, you can directly use fullName, and it will automatically update based on firstName and lastName value.

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:

  1. Caching
    Computed The property caches results, while methods is re-executed on every call.
  2. Performance
    If the logic is complex and called frequently, using the computed property can improve performance.
  3. Use Cases
    Computed The property is suitable for values derived from other data, while methods is 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 Vue3 Options API