The difference between methods and computed in Vue.js is that methods are used to define executable methods that can modify data. computed is used to define computed properties whose values are based on reactive dependencies and automatically updated when dependencies change.
##The difference between methods and computed in Vue.js
Get straight to the point: Methods and computed in Vue.js are two different property types used to handle different aspects of data.
Detailed explanation:
Methods
- Definition: is used to define executable Methods that can be used to modify or perform operations on data.
- Access: Accessed via this.
.
- Timing: Executed when the method is called.
-
Advantages:
The data can be modified. - Can perform complex logic when needed.
-
-
Disadvantages:
It will be re-executed every time it is called. - Not suitable for evaluating expressions that depend on multiple reactive data.
-
Computed
- Definition: is used to define a computed property whose value is based on an or Values for multiple reactive dependencies.
- Access: Direct access, just like a normal property.
- Timing: Recalculate when the value of a dependency changes.
-
Advantages:
Cache calculation results to avoid unnecessary recalculation. - Suitable for calculating expressions that depend on multiple reactive data.
-
-
Disadvantages:
The data cannot be modified. - Complex calculations may cause performance issues.
-
Which one to choose?
-
When using methods:
The data needs to be modified. - Need to perform complex or one-time operations.
-
-
When using computed:
Need to calculate expressions that depend on multiple reactive data. - Want to automatically update values when dependencies change.
-
The above is the detailed content of The difference between methods and computed in vue. For more information, please follow other related articles on the PHP Chinese website!