深入淺析Vue中的 computed 和 watch
computed
計(jì)算屬性:通過屬性計(jì)算得來的屬性
計(jì)算屬性,是在相關(guān)聯(lián)的屬性發(fā)生變化才計(jì)算,計(jì)算過一次,如果相關(guān)屬性沒有變化,下一次就不需要計(jì)算了,直接去緩存的值
a:<input type="number" v-model.number="a" /> b:<input type="number" v-model.number="b" /> <!--c:<input type="number" v-model.number="c" />--> 總和:{{sum()}} 總和:{{count}} 平均值:{{avg}} <p v-once>單價(jià):{{price}}</p> <p>數(shù)量:<input type="number" v-model.number="count"/></p> <p>總價(jià):{{sum}}</p> <p>運(yùn)費(fèi):{{free}}</p> <p>應(yīng)付:{{pay}}</p> data: { a: '', b:'', c:'', price: 28.8, count: '', free: 10 }, computed: { count(){ console.log('計(jì)算屬性觸發(fā)了'); return this.a+this.b; }, avg(){ return this.count/2; }, sum(){ return this.price * this.count; }, pay(){ if(this.count>0){ if(this.sum>=299){ return this.sum; }else{ return this.sum + this.free; } }else{ return 0; } } }
watch
屬性變化,就會(huì)觸發(fā)監(jiān)聽的函數(shù)。
監(jiān)聽屬性變化,一般是用于跟數(shù)據(jù)無關(guān)的業(yè)務(wù)邏輯操作。
計(jì)算屬性,適用于屬性發(fā)生變化后,需要計(jì)算得到新的數(shù)據(jù)。
<div id="app"> a: <input type="number" v-model.number="a" /><br /> b: <input type="number" v-model.number="b" /><br /> 總和:{{count}} <br /><br /><br /> name: <input type="text" v-model="obj.name" /><br /> age: <input type="text" v-model.number="obj.age" /><br /> </div> <script src="vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { a: '', b: '', count: '', obj: { name: '', age: '' } }, watch: { a(newVal, oldVal){ console.log('觸發(fā)了a-watch'); this.count = this.a + this.b; }, b(newVal){ console.log('觸發(fā)了b-watch'); this.count = this.a + this.b; }, // obj(newVal, oldVal){ // console.log('觸發(fā)了obj的監(jiān)聽'); // } // obj: { //監(jiān)聽對(duì)象中的每一個(gè)值 handler(newVal, oldVal){ console.log('觸發(fā)了obj的監(jiān)聽'); }, deep: true//深度監(jiān)聽 }, //監(jiān)聽對(duì)象中的某個(gè)屬性 'obj.name': function(){ console.log('觸發(fā)了obj.name的監(jiān)聽'); } } }) </script>
watch 也可以在methods里面進(jìn)行監(jiān)聽配置
<div id="app"> a: <input type="number" v-model.number="a" /><br /> b: <input type="number" v-model.number="b" /><br /> 總和:{{count}} <br /><br /><br /> name: <input type="text" v-model="obj.name" /><br /> age: <input type="text" v-model.number="obj.age" /><br /> <button @click="btnAction()">開始監(jiān)聽</button> </div> <script src="vue.js"></script> <script> var vm = new Vue({ el: '#app', data: { a: '', b: '', count: '', obj: { name: '', age: '' } }, methods: { btnAction(){ this.$watch('a', (newVal, oldval)=>{ console.log('監(jiān)聽到了a的變化'); console.log(newVal, oldval); }) this.$watch('obj.name', (newVal, oldval)=>{ console.log('監(jiān)聽到了obj.name的變化'); console.log(newVal, oldval); }) this.$watch('obj', (newVal, oldval)=>{ console.log('監(jiān)聽到了obj的變化'); console.log(newVal, oldval); }, { deep: true }) } } }) // vm.$watch('a', (newVal, oldval)=>{ // console.log('監(jiān)聽到了a的變化'); // console.log(newVal, oldval); // }) // // vm.$watch('obj.name', (newVal, oldval)=>{ // console.log('監(jiān)聽到了obj.name的變化'); // console.log(newVal, oldval); // }) // // vm.$watch('obj', (newVal, oldval)=>{ // console.log('監(jiān)聽到了obj的變化'); // console.log(newVal, oldval); // }, { // deep: true // }) </script>
下面在看下computed 和 watch
都可以觀察頁面的數(shù)據(jù)變化。當(dāng)處理頁面的數(shù)據(jù)變化時(shí),我們有時(shí)候很容易濫用watch。 而通常更好的辦法是使用computed屬性,而不是命令是的watch回調(diào)。
/*html: 我們要實(shí)現(xiàn) 第三個(gè)表單的值 是第一個(gè)和第二個(gè)的拼接,并且在前倆表單數(shù)值變化時(shí),第三個(gè)表單數(shù)值也在變化 */ <div id="myDiv"> <input type="text" v-model="firstName"> <input type="text" v-model="lastName"> <input type="text" v-model="fullName"> </div> <!--js: 用watch方法來實(shí)現(xiàn)--> //將需要watch的屬性定義在watch中,當(dāng)屬性變化氏就會(huì)動(dòng)態(tài)的執(zhí)行watch中的操作,并動(dòng)態(tài)的可以更新到dom中 new Vue({ el: '#myDiv', data: { firstName: 'Foo', lastName: 'Bar', fullName: 'Foo Bar' }, watch: { firstName: function (val) { this.fullName = val + ' ' + this.lastName }, lastName: function (val) { this.fullName = this.firstName + ' ' + val } } }) <!--js: 利用computed 來寫--> //計(jì)算屬性是基于它們的依賴進(jìn)行緩存的。計(jì)算屬性只有在它的相關(guān)依賴發(fā)生改變時(shí)才會(huì)重新求值。 //這就意味著只要 message 還沒有發(fā)生改變,多次訪問 reversedMessage 計(jì)算屬性會(huì)立即返回之前的計(jì)算結(jié)果,而不必再次執(zhí)行函數(shù)。 new Vue({ el:"#myDiv", data:{ firstName:"Den", lastName:"wang", }, computed:{ fullName:function(){ return this.firstName + " " +this.lastName; } } })
很容易看出 computed 在實(shí)現(xiàn)上邊的效果時(shí),是更簡單的。
總結(jié)
以上所述是小編給大家介紹的Vue中的 computed 和 watch,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Vue中computed和watch有哪些區(qū)別
- 詳解Vue中的watch和computed
- vue中watch和computed的區(qū)別與使用方法
- Vue中computed及watch區(qū)別實(shí)例解析
- Vue中的methods、watch、computed的區(qū)別
- Vue的watch和computed方法的使用及區(qū)別介紹
- vue中計(jì)算屬性(computed)、methods和watched之間的區(qū)別
- 詳解vue中computed 和 watch的異同
- Vue.js計(jì)算屬性computed與watch(5)
- 如何理解Vue中computed和watch的區(qū)別
相關(guān)文章
vue如何把字符串中的所有@內(nèi)容,替換成帶標(biāo)簽的
這篇文章主要介紹了vue如何把字符串中的所有@內(nèi)容,替換成帶標(biāo)簽的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10解決vue ui報(bào)錯(cuò)Couldn‘t parse bundle asset“C:
這篇文章主要介紹了解決vue ui報(bào)錯(cuò)Couldn‘t parse bundle asset“C:\Users\Administrator\vue_project1\dist\js\about.js“. Analyzer問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04unplugin-auto-import與unplugin-vue-components安裝問題解析
這篇文章主要為大家介紹了unplugin-auto-import與unplugin-vue-components問題解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Vue實(shí)現(xiàn)父子組件傳值其實(shí)不難
這篇文章主要介紹了Vue實(shí)現(xiàn)父子組件傳值其實(shí)不難問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03vue resource post請(qǐng)求時(shí)遇到的坑
這篇文章主要介紹了vue resource post請(qǐng)求時(shí)遇到的坑,需要的朋友可以參考下2017-10-10vue2項(xiàng)目中如何使用clipboard復(fù)制插件
這篇文章主要介紹了vue2項(xiàng)目中如何使用clipboard復(fù)制插件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07