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
<p><b>This is bold font</b></...
Table of contents What are spread and rest operat...
Table of contents Semaphore Nginx hot deployment ...
Preface This article records a common SMS verific...
This article example shares the specific code of ...
1: django-admin.py startproject project name 2: c...
Table of contents JSX environment construction Se...
Table of contents When to use Structural branches...
Environment: CentOS 7 Official documentation: htt...
The function has been implemented a long time ago...
What is a web page? The page displayed after the ...
Recently, the company is preparing to develop an ...
Learn how to host your own website on Apache, a r...
The idea of using token for login verification ...
Rendering Define the skeleton, write HTML and CSS...