vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化
更新時間:2022年04月08日 14:17:53 作者:北海之靈
這篇文章主要介紹了vue如何使用watch監(jiān)聽指定數(shù)據(jù)的變化,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
使用watch監(jiān)聽指定數(shù)據(jù)的變化
在vue中,可以使用watch屬性來監(jiān)聽data中某個屬性值的變化。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id='app'>
<input type="text" v-model="firstname" >+
<input type="text" v-model="lastname" >=
<input type="text" v-model="fullname">
</div>
</body>
<script src="../lib/vue.js"></script>
<script>
var vm = new Vue({
el:'#app',
data:{
firstname:'',
lastname:'',
fullname:''
},
methods:{
},
//使用這個屬性,可以監(jiān)視 data 中指定數(shù)據(jù)的變化,然后觸發(fā)這個 watch 中對應的function 處理函數(shù)
watch:{
firstname:function(newVal,oldVal){
//console.log('監(jiān)視到了firstname的變化');
//this.fullname = this.firstname + "-" + this.lastname
console.log(newVal +"---"+oldVal)
this.fullname = newVal + "-" + this.lastname
},
'lastname':function(newVal){
this.fullname = this.firstname + "-" + newVal
}
}
})
</script>
</html>
vue watch監(jiān)聽多個值
用computed定義一個address對象吧,然后再去watch addres
data() {
return {
check: false,
sign_img: "",
submit_flag: false'
}
},
computed: {
btnObj() {
const { sign_img, check } = this
return {
sign_img,
check
}
}
},
watch: {
btnObj: {
handler: function(newVal,oldVal) {
if(!!this.sign_img && this.check){
this.submit_flag = true
this.sign_flag = '1'
}else{
this.submit_flag = false
this.sign_flag = '0'
}
},
deep: true,
immediate: true
}
}以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
關于vue v-for 循環(huán)問題(一行顯示四個,每一行的最右邊那個計算屬性)
這篇文章主要介紹了關于vue v-for 循環(huán)問題(一行顯示四個,每一行的最右邊那個計算屬性),需要的朋友可以參考下2018-09-09
vue中手動封裝iconfont組件解析(三種引用方式的封裝和使用)
這篇文章主要介紹了vue中手動封裝iconfont組件(三種引用方式的封裝和使用),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

