vue watch監(jiān)控對(duì)象的簡單方法示例
watch的作用:監(jiān)聽vue實(shí)例上數(shù)據(jù)的變動(dòng)
示例:
queryData: {
name: '',
creator: '',
selectedStatus: '',
time: [],
},
1、普通的watch
data() {
return {
frontPoints: 0
}
},
watch: {
frontPoints(newValue, oldValue) {
console.log(newValue)
}
}
2、數(shù)組的watch
data() {
return {
winChips: new Array(11).fill(0)
}
},
watch: {
winChips: {
handler(newValue, oldValue) {
for (let i = 0; i < newValue.length; i++) {
if (oldValue[i] != newValue[i]) {
console.log(newValue)
}
}
},
deep: true
}
}
3、對(duì)象的watch
data() {
return {
bet: {
pokerState: 53,
pokerHistory: 'local'
}
}
},
watch: {
bet: {
handler(newValue, oldValue) {
console.log(newValue)
},
deep: true
}
}
tips: 只要bet中的屬性發(fā)生變化(可被監(jiān)測到的),便會(huì)執(zhí)行handler函數(shù);
如果想監(jiān)測具體的屬性變化,如pokerHistory變化時(shí),才執(zhí)行handler函數(shù),則可以利用計(jì)算屬性computed做中間層。
事例如下:
4、對(duì)象具體屬性的watch[活用computed]
data() {
return {
bet: {
pokerState: 53,
pokerHistory: 'local'
}
}
},
computed: {
pokerHistory() {
return this.bet.pokerHistory
}
},
watch: {
pokerHistory(newValue, oldValue) {
console.log(newValue)
}
}
總結(jié)
到此這篇關(guān)于vue watch監(jiān)控對(duì)象的文章就介紹到這了,更多相關(guān)vue watch監(jiān)控對(duì)象內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
ant design vue中table表格滾動(dòng)加載實(shí)現(xiàn)思路
在處理一寫數(shù)據(jù)量特別大的情況下,我們不能把后端的數(shù)據(jù)一次性全部拿到前端在table表格中展示,為了考慮性能優(yōu)化,使用了滾動(dòng)加載表格數(shù)據(jù),這篇文章主要介紹了ant design vue中table表格滾動(dòng)加載實(shí)現(xiàn)思路,需要的朋友可以參考下2024-07-07
vue對(duì)el-autocomplete二次封裝增加下拉分頁
項(xiàng)目中的聯(lián)想輸入框現(xiàn)在都是采用的el-autocomplete實(shí)現(xiàn)的,但是數(shù)據(jù)增多就會(huì)需要做分頁處理,本文主要介紹了vue對(duì)el-autocomplete二次封裝增加下拉分頁,感興趣的可以了解一下2022-03-03
vue.js如何在網(wǎng)頁中實(shí)現(xiàn)一個(gè)金屬拋光質(zhì)感的按鈕
這篇文章主要給大家介紹了關(guān)于vue.js如何在網(wǎng)頁中實(shí)現(xiàn)一個(gè)金屬拋光質(zhì)感的按鈕的相關(guān)資料,文中給出了詳細(xì)的實(shí)例代碼以及圖文將實(shí)現(xiàn)的方法介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04

