1. Introduction to computed //Basic use {{msg}} <input v-model="name" /> //Calculated properties computed:{ msg:function(){ return this.name } } In the input box, when Note: 1.1, get and set usage<input v-model="full" ><br> <input v-model="first" > <br> <input v-model="second" > data(){ return { first:'beauty', Second: 'Sister' } }, computed:{ full: get(){ //The callback function is executed when the current property value needs to be read, and the value of the current property is calculated and returned based on the relevant data return this.first + ' ' + this.second }, set(val){ //Monitor the changes of the current attribute value, execute when the attribute value changes, and update the related attribute data let names = val.split(' ') this.first = names[0] this.second = names[1] } } } get method: When set method: When the value of 1.2. Computed attribute cacheWe can also achieve this effect by splicing data through methods. The code is as follows. <div> {{ full() }} </div> data(){ return { first:'beauty', Second: 'Sister' } }, methods:{ full(){ return this.first + ' ' + this.second } }, In a page, data may be used multiple times. We implement <div> computed value: {{full}} {{full}} {{full}} {{full}} </div> <div> Methods calculate value: {{fullt}} {{fullt}} {{fullt}} {{fullt}} </div> data(){ return { first:'beauty', Second: 'Sister' } }, computed:{ full:function(){ console.log('computed') return this.first + ' ' + this.second } }, methods:{ fullt(){ console.log('method') return this.first + ' ' + this.second } } The running results are: According to the results, it is not difficult to find that the data obtained by the method needs to be recalculated several times after being used several times, but the calculated properties are not. Instead, they are cached based on their responsive dependencies and will be recalculated only when the dependent property value changes. Since it requires fewer calculations, it has better performance. 2. Introduction to watch Demo1: Monitoring simple data <input v-model="first" > <br> data(){ return { first:'beauty', } }, watch:{ first( newVal , oldVal ){ console.log('newVal',newVal) // the latest value of first console.log('oldVal',oldVal) // the previous value of first} }, // When modifying the value of first, the latest value will be printed immediately Demo2: Monitoring Object When monitoring an object, you need to use deep monitoring. <input v-model="per.name"> data(){ return { per: name:'Qianqian', age:'18' } } }, watch:{ per: handler(oldVal,newVal){ console.log('newVal',newVal) console.log('oldVal',oldVal) }, deep:true, } }, When modifying Demo3: Monitor a single property of an object // Method 1: Directly reference the object's properties <input v-model="per.name"> data(){ return { per: name:'Qianqian', age:'18' } } }, watch:{ 'per.name':function(newVal,oldVal){ console.log('newVal->',newVal) console.log('oldVal->',oldVal) } }, You can also use // Method 2: Using computed <input v-model="per.name"> data(){ return { per: name:'Qianqian', age:'18' } } }, watch:{ perName(){ console.log('name changed') } }, computed:{ perName:function(){ return this.per.name } }, Demo4: Listen to the value passed by props:{ mm:String }, //Don't use immediate watch:{ mm(newV,oldV){ console.log('newV',newV) console.log('oldV',oldV) } } //Use immediate watch:{ mm:{ immediate:true, handler(newV,oldV){ console.log('newV',newV) console.log('oldV',oldV) } } When When using The main function of 3. The difference between the two3.1. For computed
computed:{ //The property value is the function perName:function(){ return this.per.name }, //The attribute value is the attribute value full:{ get(){ }, set(val){ } } }, 3.2. For watch
When IV. Application Scenarios When numerical calculations are required and depend on other data, When you need to perform asynchronous operations or operations with high overhead when data changes, you should use Summarize: This is the end of this article about analyzing the difference between Vue's You may also be interested in:
|
<<: Nginx configuration 80 port access 8080 and project name address method analysis
>>: SQL merge operation of query results of tables with different columns
1. Command Introduction The read command is a bui...
Create a project directory mkdir php Create the f...
Brief review: Browser compatibility issues are of...
This article example shares the specific code of ...
Method 1: Modify the .bashrc or .bash_profile fil...
Preface The CentOS environment variable configura...
1. IE browser mode Hack logo 1. CSS hack logo Copy...
Preface When using the Deepin user interface, it ...
Online shopping mall database-user information da...
I have recently studied the hollowing effect. bac...
Preface Yesterday, there was a project that requi...
Table of contents 1. The origin of fork 2. Early ...
Grouping and linking in MYSQL are the two most co...
Table of contents 1. Help Command 2. Mirror comma...
Table of contents Preface optimization Derivative...