1. What is a calculated property?Expressions in templates are very convenient, but they are designed primarily for simple calculations. Putting too much logic in a template can make it cumbersome and difficult to maintain. For example: <div id="app"> { <!-- -->{ message.split('').reverse().join('') }} </div> At this point, the template is no longer just a simple declarative logic. Instead, directly reverse the string in the interpolation expression. If you use the reversed string in multiple places, it will be troublesome to write it this way and increase the consumption. So, for any complex logic, you should use computed properties. 2. Syntax of computed propertiescomputed{ function () {return //Must return a value. }Usually this function is a get function} 3. ExamplesFor the above example, we can write: <div id="app"> <p>Original string: { <!-- -->{mes}}</p> <p>Reversed string: { <!-- -->{reverseMes}}</p> </div> let vm = new Vue({ el:'#app', data:{ mes:'sayhello' }, computed: { reverseMes(){ // The calculated attribute must have a return value return this.mes.split('').reverse().join('') } } }) View the results: Here we define a function in For example, by calculating the property, the first letter of the word is capitalized: <div id="app"> <p>Original string: { <!-- -->{name}}</p> <p>Capitalize the first letter: { <!-- -->{toUpperCase}}</p> </div> In the el:"#app", data:{ name:'tom' }, // Computed properties computed:{ // Custom calculated property toUpperCase(){ return this.name.charAt(0).toUpperCase().concat(this.name.slice(1,3)) } } The output is:
For example: <div id="app1"></div> <div id="app2"> { <!-- -->{reverseMes}} </div> let vm1 = new Vue({ el:'#app1', data:{ mes:'hello' } }) let vm2 = new Vue({ el:'#app2', computed: { reverseMes(){ // Use the mes in the data center of the instance vm1 to reverse the string return vm1.mes.split('').reverse().join('') } } }) View the results: The data in the instance vm1 and vm2 can also be used to calculate the properties In addition to being used in interpolation expressions, custom computed properties can also be used in This is the end of this article about the introduction of calculated properties in Vue. For more relevant Vue calculated properties content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Table td picture horizontally and vertically centered code
>>: Detailed tutorial on installing SonarQube using Docker
The correspondence between tensorflow version and...
In MySQL, most indexes (such as PRIMARY KEY, UNIQ...
Recently I've been working on a framework tha...
Table of contents In JavaScript , there are sever...
This article mainly introduces why v-if and v-for...
Copy code The code is as follows: <HTML> &l...
When loading network data, in order to improve th...
Table of contents Preface Computed properties Int...
Mixin method: The browser cannot compile: The old...
Exposing network ports In fact, there are two par...
Introduction As mentioned in the previous article...
Main library binlog: # at 2420 #170809 17:16:20 s...
mysql full backup 1. Enable binary log and separa...
Main differences: 1. Type SQL databases are prima...
1. Perform file name search which (search for ...