About the usage of calculated properties in Vue (with code)
This article introduces to you the usage of calculated properties in Vue (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Computed properties are a very interesting thing, where you can operate the data model, and you can also use getter and setter methods. It is also very concise and clear to use
Write an example here
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> <!--<script src="js/vue.min.js"></script>--> <script src="vue.min.js"></script> </head> <body> <div id="app"> 总价:{{prices}} </div> <script> var app=new Vue({ el:'#app', data:{ package1:[ { name:'iPhone 7', price:7199, count:2 }, { name:'iPad', price:2888, count:1 } ], package2:[ { name:'apple', price:3, count:5 }, { name:'Banana', price:2, count:10 } ]}, computed:{ prices:function () { var prices=0; for(var i=0;i<this.package1.length;i++){ prices+=this.package1[i].price*this.package1[i].count; } for (var i=0;i<this.package2.length;i++){ prices+=this.package2[i].price*this.package2[i].count; } return prices; } } }) </script> </body> </html>
Define a method to calculate price in the computed attribute, and then use the data Operate the things inside
Let’s take a look at the running results:
Then let’s take a look at how to use the getter and setter methods:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> <script src="js/vue.js"></script> </head> <body> <div id="app"> 姓名:{{fullname}} </div> <script> var app=new Vue({ el:'#app', data:{ firstName:'Jack', lastName:'Green' }, computed:{ fullname:{ //getter,用于读取 get:function () { return this.firstName+ ' '+this.lastName; }, //setter set:function (newValue) { var names=newValue.split(' '); this.firstName=names[0]; this.lastName=names[names.length-1]; } } } }) </script> </body> </html>
The effect shown is like this
is also a relatively simple usage. There are also financial calculations in the shopping model. This attribute should be used more in applications
Recommended related articles:
How to convert vue.js images to Base64, upload images and preview
## How to define global variables and global methods in #vue? (Code)
The above is the detailed content of About the usage of calculated properties in Vue (with code). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

To achieve the effect of scattering and enlarging the surrounding images after clicking on the image, many web designs need to achieve an interactive effect: click on a certain image to make the surrounding...

The Y-axis position adaptive algorithm for web annotation function This article will explore how to implement annotation functions similar to Word documents, especially how to deal with the interval between annotations...

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.
