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
Required effect: After clicking to send the verif...
This article uses the element official website an...
# Adjust VMware hard disk boot priority Step 1: E...
The idea of using token for login verification ...
1. Enter the directory where your project war is ...
Table of contents 1. Common function classificati...
There are three main ways to use CSS in a page: ad...
Recently, I need to implement a cascading selecti...
Table of contents Project Introduction: Project D...
This article uses examples to illustrate the prin...
This article example shares the specific code of ...
Table of contents Stabilization Throttling Summar...
The key features of the InnoDB storage engine inc...
Quickstart Guide The Foreman installer is a colle...
Table of contents Preface 1. unknown vs any 2. Th...