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. Introduction 2. Configuratio...
Description and Introduction Docker inspect is a ...
Brief Tutorial This is a CSS3 color progress bar ...
1. How to monitor MySQL deadlocks in production e...
Table of contents 1. Synchronization Principle 2....
What you will learn 1. Software installation and ...
Brief description <br />In IE6 and 7, in a ...
Table of contents 1. MySQL compilation and instal...
NULL and NOT NULL modifiers, DEFAULT modifier, AU...
Added anti-crawler policy file: vim /usr/www/serv...
a href="#"> After clicking the link, ...
Due to the default bridge network, the IP address...
Table of contents Create a global shared content ...
Table of contents background Technical Solution S...
Table of contents Code Optimization Using key in ...