Preface: Generally, attributes are placed in For example: <div id="example"> {{ message.split('').reverse().join('') }} </div> At this point, the template is no longer just a simple declarative logic. You have to look at it for a while to realize that what we want here is to display the reverse string of the variable So, for any complex logic, you should use computed properties. 1. Basic Examples<div id="app"> <h2>Total Price: {{totalPrice}}</h2> </div> <script> const vm = new Vue({ el: "#app", data: { message: "hello", books: {name: 'Romance of the Three Kingdoms', price: 30}, {name: 'Dream of Red Mansions', price: 40}, {name: 'Journey to the West', price: 50}, {name: 'Water Margin', price: 60}, ], }, computed: { // Computed property getter totalPrice: function (){ let result = 0; // `this` refers to the vm instance for (let book of this.books) { result += book.price; } return result } } }) </script>
Here we declare a computed property Attributes generally have two methods: 2. Computed property cache vs methodYou may have noticed that we can achieve the same effect by calling methods in expressions: <div id="app"> <h2>Total price: {{getAllPrice()}}</h2> </div> <script> const vm = new Vue({ el: "#app", data: { message: "hello", books: {name: 'Romance of the Three Kingdoms', price: 30}, {name: 'Dream of Red Mansions', price: 40}, {name: 'Journey to the West', price: 50}, {name: 'Water Margin', price: 60}, ], }, methods: { getAllPrice: function () { let result = 0; // `this` refers to the vm instance for (let book of this.books) { result += book.price; } return result } }, }) </script> We can define the same function as a method instead of a computed property. The end result is indeed exactly the same either way. However, the difference is that computed properties are cached based on their reactive dependencies. They are only re-evaluated when the associated reactive dependencies change. This means that as long as
Why do we need caching? Suppose we have a computationally expensive property 3. Computed property setters Computed properties have only computed: { totalPrice: get: function () { let result = 0; // `this` refers to the vm instance for (let book of this.books) { result += book.price; } return result }, set: function (newValue) { for (let book of this.books){ book.price += 10 } } } } Here we have added a This is the end of this article about You may also be interested in:
|
<<: Explanation of the concept and usage of Like in MySQL
>>: Discussion on the way to open website hyperlinks
Table of contents 1. Data Type 1.1 Why do we need...
Often when building a SPA, you will need to prote...
I have been relearning HTML recently, which can be...
I have been working on a project recently - Budou...
Table of contents cycle for for-in for-of while d...
This article describes how to set the automatic c...
1. Installation Search the mariadb version to be ...
MySQL database storage location: 1. If MySQL uses...
If MySQL version 5.0 already exists on the machin...
MySQL encryption and decryption examples Data enc...
1. Time formatting and other methods It is recomm...
1. Search mysql in the browser to download and in...
<br />Adding pictures reasonably can make a ...
Preface The server used by the blogger was purcha...
1. Grid layout (grid): It divides the web page in...