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 Preface 1. Props, $emit one-way...
Table of contents Configure node.js+nvm+npm npm s...
Overview The builder pattern is a relatively simp...
Why do we say “usually 1em=16px”? The default tex...
1. The as keyword indicates an assertion In Types...
background There is a requirement in the project ...
What is a big deal? Transactions that run for a l...
1. Problem description <br />When JS is use...
After the user logs out, if the back button on the...
Docker is being used in more and more scenarios. ...
The find command is used to search for files in a...
Table of contents 1. Function definition 1.1 Func...
1. Download Download address: https://dev.mysql.c...
No matter you are installing Windows or Linux ope...
Use of clip-path polygon The value is composed of...