1. What is a calculated attribute? In plain words, the calculated result is stored in the attribute, which can be imagined as a cache. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <!--Note that it is currentTime1(), the brackets are for method calls.--> <p>currentTime1: {{currentTime1()}}</p> <!--The currentTime1 here is not enclosed in brackets and is called through attributes--> <p>currentTime2: {{currentTime2}}</p> </div> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.min.js"></script> <script> var vm = new Vue({ el: "#app", data: { message: "hello vue!" }, methods: { currentTime1: function () { // Return the current timestamp return Date.now(); } }, computed: { /*Note: There are calculated properties here: and the methods and computed method names cannot be the same. Only methods will be called after the same name*/ currentTime2: function () { this.message; // Return the current timestamp return Date.now(); } } }); </script> </body> </html> Running results: Maybe at first glance there seems to be no difference. But if we think about it carefully, one is a method and the other is a property. As shown below: <div id="app"> <!--Note that it is currentTime1(), the brackets are for method calls.--> <p>currentTime1: {{currentTime1()}}</p> <!--The currentTime1 here is not enclosed in brackets and is called through attributes--> <p>currentTime2: {{currentTime2}}</p> </div> Key point : Attributes store values. They will change only when new values come in. Otherwise, it will be the same as caching. Let's look at this: To explain: 1. From 1 and 2, we can see that one is a method and the other is a property. It is definitely not possible to call a property using a method. 2. In 1 and 3, we can see that the value of the method call is constantly changing, but the value of the attribute call is not changing. This is the same as the cache mechanism. 3. From 3, 4, and 5, we can see that once we change the value in the function, it is equivalent to changing the value in the cache, and then it will refresh the value. SummarizeWhen a method is called, calculations need to be performed each time. Since there is a calculation process, system overhead will inevitably be generated. What if the result does not change frequently? At this point, you can consider caching the result. This is easily done using computer properties. The main feature of computer properties is to cache calculation results that do not change frequently to save our system overhead. -----Do you understand, my friends? This article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: In-depth understanding of Linux load balancing LVS
>>: Sharing tips on using scroll bars in HTML
Table of contents 1. Overview 2. Memory Managemen...
Scenario Requirements 1. We can use the script fu...
question: The commonly used command "ll"...
The results are different in Windows and Linux en...
1. OpenSSL official website Official download add...
The paging effect is implemented in the vue proje...
This article shares the specific code of js to re...
Nginx's rewrite function supports regular mat...
Table of contents 1. Project Description 2. Nginx...
1.1. Download: Download the zip package from the ...
Table of contents Main topic 1. Install Docker on...
The span tag is often used when making HTML web pa...
view: Views in MySQL have many similarities with ...
HTML img tag: defines an image to be introduced in...
1. Background Netplan is a new command-line netwo...