Vue監(jiān)聽屬性和計(jì)算屬性
一、watch監(jiān)聽屬性
這個屬性用來監(jiān)視某個數(shù)據(jù)的變化,并觸發(fā)相應(yīng)的回調(diào)函數(shù)執(zhí)行。
基本用法添加watch
屬性,值為一個對象。對象的屬性名就是要監(jiān)視的數(shù)據(jù),屬性值為回調(diào)函數(shù),每當(dāng)這個屬性名對應(yīng)的值發(fā)生變化,就會觸發(fā)該回調(diào)函數(shù)執(zhí)行。
回調(diào)函數(shù)有2個參數(shù):
newVal
,數(shù)據(jù)發(fā)生改變后的值。oldVal
,數(shù)據(jù)發(fā)生改變前的值。
計(jì)數(shù)器:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <div>計(jì)數(shù)器{{ shu }}</div> <span>改變前: {{ov}}</span> <span>改變后: {{nv}}</span> <br /> <button @click="shu++">加一</button> </div> <script> var vm = new Vue({ el: '#app', data: { shu:1, ov:0, nv:0 }, methods:{ } }) vm.$watch("shu",function(nval,oval){ vm.$data.ov = oval vm.$data.nv = nval }) </script> </body> </html>
添加監(jiān)聽,同時將計(jì)數(shù)器改變前的值賦值給變量ov
,將改變后的值賦值給變量nv
vm.$watch("shu",function(nval,oval){ vm.$data.ov = oval vm.$data.nv = nval })
1.
1.購物車
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <table> <tr> <th>序號</th> <th>商品名稱</th> <th>商品價格</th> <th>購買數(shù)量</th> <th>操作</th> </tr> <tr v-for="sp in sps"> <td>{{ sp.id }}</td> <td>{{ sp.name }}</td> <td>{{ sp.money }}</td> <td> <button v-on:click="sp.sum=sp.sum-1">-</button> {{ sp.sum }} <button v-on:click="sp.sum=sp.sum+1">+</button> </td> <td> <button v-on:click="sp.sum=0">重置</button> </td> </tr> </table> <div > 總價:{{ getmaney() }} </div> </div> <script> var vm = new Vue({ el: '#app', data: { sps:[ { id:1, name:"蘋果13", money:6000, sum:1 }, { id:2, name:"蘋果12", money:5000, sum:1 }, { id:3, name:"蘋果11", money:4000, sum:1 } ] }, methods:{ getmaney:function(){ var m=0; for(var x=0;x<this.sps.length;x++){ m=m+this.sps[x].money*this.sps[x].sum; } return m; } } }) </script> </body> </html>
得出總的費(fèi)用:
getmaney:function(){ var m=0; for(var x=0;x<this.sps.length;x++){ m=m+this.sps[x].money*this.sps[x].sum; } return m; }
2.全選與取消全選
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <input type="checkbox" id="a" value="a" v-model="che"/> <label for="a">a</label> <input type="checkbox" id="b" value="b" v-model="che"/> <label for="b">b</label> <input type="checkbox" v-model="checked" id="bok" v-on:change="ckall()" /> <label for="box">全選</label> </div> <script> var vm = new Vue({ el: '#app', data: { checked:false, che:[], shuzu:["a","b"] }, methods:{ ckall:function(){ //全選狀態(tài) if(this.checked){ this.che = this.shuzu }else{ //取消全選 this.che=[] } } }, watch:{ "che":function(){ //判斷是否全選 if(this.che.length == this.shuzu.length){ this.checked=true }else{ this.checked=false } } } }) </script> </body> </html>
二、計(jì)算屬性
1.computed
特點(diǎn):
- 在
computed
屬性對象中定義計(jì)算屬性的方法,在頁面上使用{{方法名}}來顯示計(jì)算結(jié)果。 - 通過
getter/setter
來顯示和監(jiān)視屬性數(shù)據(jù)。 - 計(jì)算屬性存在緩存,多次讀取只執(zhí)行一次
getter
。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <p>{{ mess }}</p> <p>{{ remess }}</p> <p>{{ mess.split('').reverse().join('') }}</p> </div> <script> var vm = new Vue({ el: '#app', data:{ mess:'abcd' }, computed: { remess:function(){ return this.mess.split('').reverse().join('') } } }) </script> </body> </html>
2.methods
computed
基于它的依賴緩存,只有相關(guān)依賴發(fā)生改變時才會重新取值。而使用methods
,在重新渲染的時候,函數(shù)總會重新調(diào)用執(zhí)行。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <input v-model="mess" /> <p>{{ mess }}</p> <p>{{ remess }}</p> <p>{{ remess2() }}</p> </div> <script> var vm = new Vue({ el: '#app', data:{ mess:'abcd' }, computed: { remess:function(){ return this.mess.split('').reverse().join('') } }, methods: { remess2:function(){ return this.mess.split('').reverse().join('') } } }) </script> </body> </html>
3.setter
當(dāng)頁面獲取某個數(shù)據(jù)的時候,先會在data里面找,找不到就會去計(jì)算屬性里面找,在計(jì)算屬性里獲取數(shù)據(jù)時會自動執(zhí)行g(shù)et方法,同時也提供了set方法。computed
屬性默認(rèn)只有getter
,不過在需要時也可以提供一個setter。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="./js/vue.js"></script> </head> <body> <div id="app"> <p>{{ site }}</p> </div> <script> var vm = new Vue({ el: '#app', data: { name: 'xiaowang', cls:'01' }, computed:{ site:{ get: function(){ return this.name+' '+this.cls }, set: function(Value){ var names = Value.split('|') this.name = names[0] this.cls = names[1] } } } }) vm.site = 'xiaowang|01'; document.write('name:'+vm.name); document.write('<br>'); document.write('class:'+vm.cls); </script> </body> </html>
到此這篇關(guān)于Vue監(jiān)聽屬性和計(jì)算屬性的文章就介紹到這了,更多相關(guān)Vue監(jiān)聽和計(jì)算屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE對Storage的過期時間設(shè)置,及增刪改查方式
這篇文章主要介紹了VUE對Storage的過期時間設(shè)置,及增刪改查方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02vue如何通過id從列表頁跳轉(zhuǎn)到對應(yīng)的詳情頁
這篇文章主要介紹了vue如何通過id從列表頁跳轉(zhuǎn)到對應(yīng)的詳情頁 ,需要的朋友可以參考下2018-05-05vue基礎(chǔ)之data存儲數(shù)據(jù)及v-for循環(huán)用法示例
這篇文章主要介紹了vue基礎(chǔ)之data存儲數(shù)據(jù)及v-for循環(huán)用法,結(jié)合實(shí)例形式分析了vue.js使用data存儲數(shù)據(jù)、讀取數(shù)據(jù)及v-for遍歷數(shù)據(jù)相關(guān)操作技巧,需要的朋友可以參考下2019-03-03深入淺析Vue中mixin和extend的區(qū)別和使用場景
Vue中有兩個較為高級的靜態(tài)方法mixin和extend,接下來給大家介紹Vue中mixin和extend的區(qū)別和使用場景,感興趣的朋友一起看看吧2019-08-08vuex狀態(tài)管理數(shù)據(jù)狀態(tài)查詢與更改方式
這篇文章主要介紹了vuex狀態(tài)管理數(shù)據(jù)狀態(tài)查詢與更改方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04Element Carousel 走馬燈的具體實(shí)現(xiàn)
這篇文章主要介紹了Element Carousel 走馬燈的具體實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07vue-cli的webpack模板項(xiàng)目配置文件分析
本篇文章主要對vue-cli的webpack模板項(xiàng)目配置文件進(jìn)行分析。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04