When calculating variables in Vue, which is better, methods or computed?
When calculating variables in Vue, we usually use two methods: methods and computed. Although both can be used to calculate variables, there are still some differences in their use. This article will introduce in detail the differences between methods and computed and which method to use in what circumstances.
methods:
Methods can be understood as a method library of Vue, in which we can define some methods and then call these methods in the template. When we need to calculate some more complex variables, we can use methods to perform a series of calculations. For example, the following example:
<template>
<div>
<p>{{reverseMsg}}</p>
</div>
</template>
<script>
export default {
data() {
return {
message: 'hello'
}
},
methods: {
reverseMsg(){
return this.message.split('').reverse().join('')
}
}
}
</script>
In the above example, we want to reverse the string of message, so we define a reverseMsg
method to achieve this function. Calling the method in the template reverseMsg
can get the reversed string.
computed:
Computed can be understood as a calculated property of Vue. We can define some calculated properties in it and then call these calculated properties in the template. When we need to calculate some data-related variables, we can use computed to calculate. For example, the following example:
<template>
<div>
<p>{{reversedMsg}}</p>
</div>
</template>
<script>
export default {
data() {
return {
message: 'hello'
}
},
computed: {
reversedMsg() {
return this.message.split('').reverse().join('')
}
}
}
</script>
In the above example, we also want to reverse the string of message, but we use computed to achieve this function. We define reversedMsg as a computed property, and then call it in the template to get the reversed string.
Which is better, methods or computed?
Both methods can be used to calculate variables, but in actual use, you still have to choose according to the specific situation. If we need to do more complex calculations, or need to perform some asynchronous operations, we need to use methods to implement them. If we need to do some simpler calculations, or need to monitor the changes of a certain attribute to refresh the variable in real time, we need to use computed to implement it.
In general, if you can use computed to calculate variables, use computed as much as possible , because computed will automatically update the calculation after the data changes, while methods will re-call the method. However, in some special cases, such as asynchronous operations, it is more convenient to use methods .
Summarize
In Vue, both methods and computed variables can be used to achieve our needs, but we still have to choose which method to use based on the specific situation. The difference between methods and computed properties mainly exists in the calculation process. Computed properties will cache the calculation results and implement responsive updates based on the properties, while methods will re-execute the calculation.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Configuring Apache Web Server on Ubuntu and Debian
Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM
-
This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
My understanding of webservice is this
Publish Date:2025/03/18 Views:150 Category:NETWORK
-
Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat
Which technology do you choose to implement the web chat room?
Publish Date:2025/03/18 Views:62 Category:NETWORK
-
With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio
How to redirect a website from HTTP to HTTPS
Publish Date:2025/03/16 Views:117 Category:NETWORK
-
HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi
How to avoid soft 404 in the website during SEO
Publish Date:2025/03/17 Views:136 Category:NETWORK
-
This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand
Performance differences between HTTP REST API and WebSocket REST API
Publish Date:2025/03/17 Views:161 Category:NETWORK
-
What is a REST API? REST or RESTful API design (Representational State Transfer) is an architectural style that uses existing protocols. There are six key constraints for REST API design:
Webpack packages ES6 and CommonJs mixed React
Publish Date:2025/03/02 Views:179 Category:React
-
This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.
Details that need to be paid attention to when compiling react with webpack
Publish Date:2025/03/02 Views:201 Category:React
-
It is very convenient to compile and package react using webpack. Both need to be installed using npm. However, if you do not pay attention to the installation location of webpack and react parser babel during use, problems will occur during