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
Deploy the project to the project site test envir...
Linux version: CentOS 7 [root@azfdbdfsdf230lqdg1b...
Preface When I was studying the front end before,...
Table of contents Introduction to NIS Network env...
KILL [CONNECTION | QUERY] processlist_id In MySQL...
This article shares the specific code of JS to re...
Table of contents 1. Basic principles 2. Specific...
This article example shares the specific code of ...
This article shares the specific code for JavaScr...
Table of contents 1. Home Page Production 1. Prod...
Table of contents 1. Environmental Preparation 2....
I didn't use MySQL very often before, and I w...
After installing a centos8 service under vmware a...
Today I found this prompt when I was running and ...
Introduction to NFS NFS (Network File System) is ...