分析 Vue 中的 computed 和 watch 的區(qū)別
一、computed介紹
computed
用來(lái)監(jiān)控自己定義的變量,該變量在 data
內(nèi)沒(méi)有聲明,直接在 computed
里面定義,頁(yè)面上可直接使用。
//基礎(chǔ)使用 {{msg}} <input v-model="name" /> //計(jì)算屬性 computed:{ msg:function(){ return this.name } }
在輸入框中,改變 name
值得時(shí)候,msg
也會(huì)跟著改變。這是因?yàn)?computed
監(jiān)聽(tīng)自己的屬性 msg
,發(fā)現(xiàn) name
一旦變動(dòng),msg 立馬會(huì)更新。
注意:msg
不可在 data
中定義,否則會(huì)報(bào)錯(cuò)。
1.1、get 和 set 用法
<input v-model="full" ><br> <input v-model="first" > <br> <input v-model="second" > data(){ return{ first:'美女', second:'姐姐' } }, computed:{ full:{ get(){ //回調(diào)函數(shù) 當(dāng)需要讀取當(dāng)前屬性值是執(zhí)行,根據(jù)相關(guān)數(shù)據(jù)計(jì)算并返回當(dāng)前屬性的值 return this.first + ' ' + this.second }, set(val){ //監(jiān)視當(dāng)前屬性值的變化,當(dāng)屬性值發(fā)生變化時(shí)執(zhí)行,更新相關(guān)的屬性數(shù)據(jù) let names = val.split(' ') this.first = names[0] this.second = names[1] } } }
get 方法:first
和 second
改變時(shí),會(huì)調(diào)用 get
方法,更新 full
的值。
set 方法:修改 full
的值時(shí),會(huì)調(diào)用 set
方法,val
是 full
的最新值。
1.2、計(jì)算屬性緩存
我們通過(guò)方法,拼接數(shù)據(jù),也可以實(shí)現(xiàn)該效果,代碼如下。
<div> {{ full() }} </div> data(){ return{ first:'美女', second:'姐姐' } }, methods:{ full(){ return this.first + ' ' + this.second } },
一個(gè)頁(yè)面內(nèi),數(shù)據(jù)有可能多次使用,我們把 computed
和 method
兩個(gè)方法放一起實(shí)現(xiàn),并把這個(gè)數(shù)據(jù)在頁(yè)面內(nèi)多次引用,試看以下效果。
<div> computed計(jì)算值: {{full}} {{full}} {{full}} {{full}} </div> <div> methods計(jì)算值: {{fullt}} {{fullt}} {{fullt}} {{fullt}} </div> data(){ return{ first:'美女', second:'姐姐' } }, computed:{ full:function(){ console.log('computed') return this.first + ' ' + this.second } }, methods:{ fullt(){ console.log('方法') return this.first + ' ' + this.second } }
運(yùn)行結(jié)果為:
根據(jù)結(jié)果,我們不難發(fā)現(xiàn),根據(jù)方法獲取到的數(shù)據(jù),使用幾次就需要重新計(jì)算幾次,但計(jì)算屬性不是,而是基于它們的響應(yīng)式依賴進(jìn)行緩存的,之后依賴屬性值發(fā)生改變的時(shí)候,才會(huì)重新計(jì)算。由于它計(jì)算次數(shù)少,所以性能更高些。
二、watch介紹
watch
是監(jiān)測(cè) Vue 實(shí)例上的數(shù)據(jù)變動(dòng),通俗地講,就是檢測(cè) data
內(nèi)聲明的數(shù)據(jù)。不僅可以監(jiān)測(cè)簡(jiǎn)單數(shù)據(jù),還可以監(jiān)測(cè)對(duì)象或?qū)ο髮傩浴?/p>
Demo1:監(jiān)測(cè)簡(jiǎn)單數(shù)據(jù)
<input v-model="first" > <br> data(){ return{ first:'美女', } }, watch:{ first( newVal , oldVal ){ console.log('newVal',newVal) // first 的最新值 console.log('oldVal',oldVal) // first上一個(gè)值 } }, // 修改 first的值的時(shí)候,立馬會(huì)打印最新值
Demo2:監(jiān)測(cè)對(duì)象
監(jiān)聽(tīng)對(duì)象的時(shí)候,需要使用深度監(jiān)聽(tīng)。
<input v-model="per.name"> data(){ return{ per:{ name:'倩倩', age:'18' } } }, watch:{ per:{ handler(oldVal,newVal){ console.log('newVal',newVal) console.log('oldVal',oldVal) }, deep:true, } },
修改 per.name
的時(shí)候,發(fā)現(xiàn) newVal
和 oldVal
的值是一樣的,是因?yàn)樗麄兇鎯?chǔ)的指針指向的是同一個(gè)地方,所以深度監(jiān)聽(tīng)雖然可以監(jiān)聽(tīng)到對(duì)象的變化,但是無(wú)法監(jiān)聽(tīng)到具體的是哪個(gè)屬性發(fā)生變化了。
Demo3:監(jiān)聽(tīng)對(duì)象的單個(gè)屬性
// 方法1:直接引用對(duì)象的屬性 <input v-model="per.name"> data(){ return{ per:{ name:'倩倩', age:'18' } } }, watch:{ 'per.name':function(newVal,oldVal){ console.log('newVal->',newVal) console.log('oldVal->',oldVal) } },
也可以借助 computed
作為中間轉(zhuǎn)換,如下:
// 方法2:借助computed <input v-model="per.name"> data(){ return{ per:{ name:'倩倩', age:'18' } } }, watch:{ perName(){ console.log('name改變了') } }, computed:{ perName:function(){ return this.per.name } },
Demo4:監(jiān)聽(tīng) props
組件傳過(guò)來(lái)的值
props:{ mm:String }, //不使用 immediate watch:{ mm(newV,oldV){ console.log('newV',newV) console.log('oldV',oldV) } } //使用 immediate watch:{ mm:{ immediate:true, handler(newV,oldV){ console.log('newV',newV) console.log('oldV',oldV) } }
不使用 immediate
時(shí),第一次加載頁(yè)面時(shí),watch
監(jiān)聽(tīng)的 mm
中的打印并沒(méi)有執(zhí)行。
使用 immediate
時(shí),第一次加載時(shí)也會(huì)打印結(jié)果:newV 11 oldV undefined
。
immediate
主要作用就是組件加載時(shí),會(huì)立即觸發(fā)回調(diào)函數(shù)。
三、兩者區(qū)別
3.1、對(duì)于 computed
computed
監(jiān)控的數(shù)據(jù)在data
中沒(méi)有聲明computed
不支持異步,當(dāng)computed
中有異步操作時(shí),無(wú)法監(jiān)聽(tīng)數(shù)據(jù)的變化computed
具有緩存,頁(yè)面重新渲染,值不變時(shí),會(huì)直接返回之前的計(jì)算結(jié)果,不會(huì)重新計(jì)算- 如果一個(gè)屬性是由其他屬性計(jì)算而來(lái)的,這個(gè)屬性依賴其他屬性,一般使用
computed
computed
計(jì)算屬性值是函數(shù)時(shí),默認(rèn)使用get
方法。如果屬性值是屬性值時(shí),屬性有一個(gè)get
和set
方法,當(dāng)數(shù)據(jù)發(fā)生變化時(shí)會(huì)調(diào)用set
方法
computed:{ //屬性值為函數(shù) perName:function(){ return this.per.name }, //屬性值為屬性值 full:{ get(){ }, set(val){ } } },
3.2、對(duì)于 watch
- 監(jiān)測(cè)的數(shù)據(jù)必須在
data
中聲明或props
中數(shù)據(jù) - 支持異步操作
- 沒(méi)有緩存,頁(yè)面重新渲染時(shí),值不改變時(shí)也會(huì)執(zhí)行
- 當(dāng)一個(gè)屬性值發(fā)生變化時(shí),就需要執(zhí)行相應(yīng)的操作
- 監(jiān)聽(tīng)數(shù)據(jù)發(fā)生變化時(shí),會(huì)觸發(fā)其他操作,函數(shù)有兩個(gè)參數(shù):
immediate
:組件加載立即觸發(fā)回調(diào)函數(shù)
deep
:深度監(jiān)聽(tīng),主要針對(duì)復(fù)雜數(shù)據(jù),如監(jiān)聽(tīng)對(duì)象時(shí),添加深度監(jiān)聽(tīng),任意的屬性值改變都會(huì)觸發(fā)。
注意:對(duì)象添加深度監(jiān)聽(tīng)之后,輸出的新舊值是一樣的。
computed
頁(yè)面重新渲染時(shí),不會(huì)重復(fù)計(jì)算,而 watch
會(huì)重新計(jì)算,所以 computed
性能更高些。
四、應(yīng)用場(chǎng)景
當(dāng)需要進(jìn)行數(shù)值計(jì)算,并且依賴于其它數(shù)據(jù)時(shí),應(yīng)該使用 computed
,因?yàn)榭梢岳?computed
的緩存特性,避免每次獲取值時(shí)都要重新計(jì)算。
當(dāng)需要在數(shù)據(jù)變化時(shí)執(zhí)行異步操作或開(kāi)銷較大的操作時(shí),應(yīng)該使用 watch
,computed
不支持異步。如果需要限制執(zhí)行操作的頻率,可借助 computed
作為中間狀態(tài)。
總結(jié):
到此這篇關(guān)于分析 Vue 的 computed
和 watch
的區(qū)別的文章就介紹到這了,更多相關(guān)Vue 的 computed
和 watch
的區(qū)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue中computed和watch的區(qū)別小結(jié)
- 一文搞懂Vue中computed和watch的區(qū)別
- Vue 基礎(chǔ)語(yǔ)法之計(jì)算屬性(computed)、偵聽(tīng)器(watch)、過(guò)濾器(filters)詳解
- Vue?中的?computed?和?watch?的區(qū)別詳解
- Vue3中的?computed,watch,watchEffect的使用方法
- 詳解Vue中Computed與watch的用法與區(qū)別
- 關(guān)于Vue的 watch、computed和methods的區(qū)別匯總
- Vue computed與watch用法區(qū)分
相關(guān)文章
vue-cli3+ts+webpack實(shí)現(xiàn)多入口多出口功能
這篇文章主要介紹了vue-cli3+ts+webpack實(shí)現(xiàn)多入口多出口功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-05-05Tree 組件搜索過(guò)濾功能實(shí)現(xiàn)干貨
這篇文章主要為大家介紹了 Tree組件搜索過(guò)濾功能實(shí)現(xiàn)干貨詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07解決vue初始化項(xiàng)目時(shí),一直卡在Project description上的問(wèn)題
今天小編就為大家分享一篇解決vue初始化項(xiàng)目時(shí),一直卡在Project description上的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10vue實(shí)現(xiàn)簡(jiǎn)潔文件上傳進(jìn)度條功能
這篇文章主要介紹了vue實(shí)現(xiàn)簡(jiǎn)潔文件上傳進(jìn)度條功能,實(shí)現(xiàn)原理是通過(guò)performance.now()獲取動(dòng)畫(huà)的時(shí)間戳,用于創(chuàng)建流暢的動(dòng)畫(huà),結(jié)合示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06