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
Table of contents 1. Pull the image 2. Create a l...
When there is a lot of data to be displayed, the ...
All content in this blog is licensed under Creati...
1. What are custom hooks Logic reuse Simply put, ...
The overall architecture of MySQL is divided into...
Apache Arrow is a popular format used by various ...
The drag function is mainly used to allow users t...
Prepare a CentOS6 installation disk (any version)...
Table of contents EffectList Collection EffectLis...
In actual development, the primary key of MySQL c...
Volume Label, Property Name, Description 002 <...
Without further ado, I will post the code for you...
Preface We all know that the import and export of...
Table of contents vue2.x Pre-concept: Routing hoo...
Possible solutions 1. Math.random generates rando...