Vue使用watch監(jiān)聽數(shù)組或?qū)ο?/h1>
更新時間:2022年06月26日 11:29:54 作者:小旭2021
這篇文章介紹了Vue使用watch監(jiān)聽數(shù)組或?qū)ο蟮姆椒?,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
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、對象的watch
data() {
return {
bet: {
pokerState: 53,
pokerHistory: 'local'
}
}
},
watch: {
bet: {
handler(newValue, oldValue) {
console.log(newValue)
},
deep: true
}
}
tips: 只要bet中的屬性發(fā)生變化(可被監(jiān)測到的),便會執(zhí)行handler函數(shù);
如果想監(jiān)測具體的屬性變化,如pokerHistory變化時,才執(zhí)行handler函數(shù),則可以利用計算屬性computed做中間層。
事例如下:
4、對象具體屬性的watch[活用computed]
data() {
return {
bet: {
pokerState: 53,
pokerHistory: 'local'
}
}
},
computed: {
pokerHistory() {
return this.bet.pokerHistory
}
},
watch: {
pokerHistory(newValue, oldValue) {
console.log(newValue)
}
}
到此這篇關(guān)于Vue使用watch監(jiān)聽數(shù)組或?qū)ο蟮奈恼戮徒榻B到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
-
vue在標(biāo)簽中如何使用(data-XXX)自定義屬性并獲取
這篇文章主要介紹了vue在標(biāo)簽中如何使用(data-XXX)自定義屬性并獲取,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教 2022-08-08
-
一文詳解Vue如何整合Echarts實現(xiàn)可視化界面
ECharts,縮寫來自Enterprise Charts,商業(yè)級數(shù)據(jù)圖表,一個純Javascript的圖表庫,可以流暢的運(yùn)行在PC和移動設(shè)備上。本文將在Vue中整合Echarts實現(xiàn)可視化界面,感興趣的可以了解一下 2022-04-04
-
Element-ui的table中使用fixed后出現(xiàn)行混亂情況的解決
這篇文章主要介紹了Element-ui的table中使用fixed后出現(xiàn)行混亂情況的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教 2023-10-10
最新評論
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、對象的watch
data() { return { bet: { pokerState: 53, pokerHistory: 'local' } } }, watch: { bet: { handler(newValue, oldValue) { console.log(newValue) }, deep: true } }
tips: 只要bet中的屬性發(fā)生變化(可被監(jiān)測到的),便會執(zhí)行handler函數(shù);
如果想監(jiān)測具體的屬性變化,如pokerHistory變化時,才執(zhí)行handler函數(shù),則可以利用計算屬性computed做中間層。
事例如下:
4、對象具體屬性的watch[活用computed]
data() { return { bet: { pokerState: 53, pokerHistory: 'local' } } }, computed: { pokerHistory() { return this.bet.pokerHistory } }, watch: { pokerHistory(newValue, oldValue) { console.log(newValue) } }
到此這篇關(guān)于Vue使用watch監(jiān)聽數(shù)組或?qū)ο蟮奈恼戮徒榻B到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue在標(biāo)簽中如何使用(data-XXX)自定義屬性并獲取
這篇文章主要介紹了vue在標(biāo)簽中如何使用(data-XXX)自定義屬性并獲取,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08一文詳解Vue如何整合Echarts實現(xiàn)可視化界面
ECharts,縮寫來自Enterprise Charts,商業(yè)級數(shù)據(jù)圖表,一個純Javascript的圖表庫,可以流暢的運(yùn)行在PC和移動設(shè)備上。本文將在Vue中整合Echarts實現(xiàn)可視化界面,感興趣的可以了解一下2022-04-04Element-ui的table中使用fixed后出現(xiàn)行混亂情況的解決
這篇文章主要介紹了Element-ui的table中使用fixed后出現(xiàn)行混亂情況的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10