關(guān)于Vue中的watch監(jiān)視屬性
一、監(jiān)視屬性watch
1.當被監(jiān)視的屬性變化時,回調(diào)函數(shù)自動調(diào)用,進行相關(guān)操作
2.監(jiān)視的屬性必須存在,才能進行監(jiān)視
3.監(jiān)視的兩種寫法
(1)new Vue時傳入watch配置
(2)通過vm.$watch監(jiān)視
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>天氣案例_監(jiān)視屬性</title>
<!-- 引入Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!--
監(jiān)視屬性watch:
1.當被監(jiān)視的屬性變化時, 回調(diào)函數(shù)自動調(diào)用, 進行相關(guān)操作
2.監(jiān)視的屬性必須存在,才能進行監(jiān)視?。?
3.監(jiān)視的兩種寫法:
(1).new Vue時傳入watch配置
(2).通過vm.$watch監(jiān)視
-->
<!-- 準備好一個容器-->
<div id="root">
<h2>今天天氣很{{info}}</h2>
<button @click="changeWeather">切換天氣</button>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在啟動時生成生產(chǎn)提示。
const vm = new Vue({
el:'#root',
data:{
isHot:true,
},
computed:{
info(){
return this.isHot ? '炎熱' : '涼爽'
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
/* watch:{
isHot:{
immediate:true, //初始化時讓handler調(diào)用一下
//handler什么時候調(diào)用?當isHot發(fā)生改變時。
handler(newValue,oldValue){
console.log('isHot被修改了',newValue,oldValue)
}
}
} */
})
vm.$watch('isHot',{
immediate:true, //初始化時讓handler調(diào)用一下
//handler什么時候調(diào)用?當isHot發(fā)生改變時。
handler(newValue,oldValue){
console.log('isHot被修改了',newValue,oldValue)
}
})
</script>
</html>二、深度監(jiān)視
1.Vue中的watch默認不監(jiān)視對象內(nèi)部值的改變(一層)
2.配置deep:true可以監(jiān)視對象內(nèi)部值的改變(多層)
備注:
- vue自身可以監(jiān)視對象內(nèi)部值的改變,但vue提供的watch默認不可以
- 使用watch時根據(jù)數(shù)據(jù)的具體結(jié)構(gòu),決定是否采用深度監(jiān)視
天氣案例:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>天氣案例_深度監(jiān)視</title>
<!-- 引入Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!--
深度監(jiān)視:
(1).Vue中的watch默認不監(jiān)測對象內(nèi)部值的改變(一層)。
(2).配置deep:true可以監(jiān)測對象內(nèi)部值改變(多層)。
備注:
(1).Vue自身可以監(jiān)測對象內(nèi)部值的改變,但Vue提供的watch默認不可以!
(2).使用watch時根據(jù)數(shù)據(jù)的具體結(jié)構(gòu),決定是否采用深度監(jiān)視。
-->
<!-- 準備好一個容器-->
<div id="root">
<h2>今天天氣很{{info}}</h2>
<button @click="changeWeather">切換天氣</button>
<hr/>
<h3>a的值是:{{numbers.a}}</h3>
<button @click="numbers.a++">點我讓a+1</button>
<h3>b的值是:{{numbers.b}}</h3>
<button @click="numbers.b++">點我讓b+1</button>
<button @click="numbers = {a:666,b:888}">徹底替換掉numbers</button>
{{numbers.c.d.e}}
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在啟動時生成生產(chǎn)提示。
const vm = new Vue({
el:'#root',
data:{
isHot:true,
numbers:{
a:1,
b:1,
c:{
d:{
e:100
}
}
}
},
computed:{
info(){
return this.isHot ? '炎熱' : '涼爽'
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
watch:{
isHot:{
// immediate:true, //初始化時讓handler調(diào)用一下
//handler什么時候調(diào)用?當isHot發(fā)生改變時。
handler(newValue,oldValue){
console.log('isHot被修改了',newValue,oldValue)
}
},
//監(jiān)視多級結(jié)構(gòu)中某個屬性的變化
/* 'numbers.a':{
handler(){
console.log('a被改變了')
}
} */
//監(jiān)視多級結(jié)構(gòu)中所有屬性的變化
numbers:{
deep:true,
handler(){
console.log('numbers改變了')
}
}
}
})
</script>
</html>三、監(jiān)視屬性簡寫
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>天氣案例_監(jiān)視屬性_簡寫</title>
<!-- 引入Vue -->
<script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
<!-- 準備好一個容器-->
<div id="root">
<h2>今天天氣很{{info}}</h2>
<button @click="changeWeather">切換天氣</button>
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止 vue 在啟動時生成生產(chǎn)提示。
const vm = new Vue({
el:'#root',
data:{
isHot:true,
},
computed:{
info(){
return this.isHot ? '炎熱' : '涼爽'
}
},
methods: {
changeWeather(){
this.isHot = !this.isHot
}
},
watch:{
//正常寫法
/* isHot:{
// immediate:true, //初始化時讓handler調(diào)用一下
// deep:true,//深度監(jiān)視
handler(newValue,oldValue){
console.log('isHot被修改了',newValue,oldValue)
}
}, */
//簡寫
/* isHot(newValue,oldValue){
console.log('isHot被修改了',newValue,oldValue,this)
} */
}
})
//正常寫法
/* vm.$watch('isHot',{
immediate:true, //初始化時讓handler調(diào)用一下
deep:true,//深度監(jiān)視
handler(newValue,oldValue){
console.log('isHot被修改了',newValue,oldValue)
}
}) */
//簡寫
/* vm.$watch('isHot',(newValue,oldValue)=>{
console.log('isHot被修改了',newValue,oldValue,this)
}) */
</script>
</html>到此這篇關(guān)于關(guān)于Vue中的watch監(jiān)視屬性的文章就介紹到這了,更多相關(guān)Vue中的watch監(jiān)視屬性內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue-cli集成axios請求出現(xiàn)CORS跨域問題及解決
這篇文章主要介紹了Vue-cli集成axios請求出現(xiàn)CORS跨域問題及解決方案,具有很好的參考價值,希望對大家有所幫助,2023-10-10
Vue父組件和子組件之間數(shù)據(jù)傳遞和方法調(diào)用
vue組件在通信中,無論是子組件向父組件傳值還是父組件向子組件傳值,他們都有一個共同點就是有中間介質(zhì),子向父的介質(zhì)是自定義事件,父向子的介質(zhì)是props中的屬性。2022-12-12
Vue 中如何將函數(shù)作為 props 傳遞給組件的實現(xiàn)代碼
這篇文章主要介紹了Vue 中如何將函數(shù)作為 props 傳遞給組件的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
vxe-list?vue?如何實現(xiàn)下拉框的虛擬列表
這篇文章主要介紹了vxe-list?vue?如何實現(xiàn)下拉框的虛擬列表,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06

