使用计算属性computed,监听计算属性
data () {
return {
name1: '',
name2: '',
age: ''
}
},
computed: {
listenChange () {
const {name1,name2} = this
return {name1,name2}
}
},
watch: {
listenChange (val) {
console.log('listenChange :', val)
if (val.name1 === val.name2) {
this.$set(this,'age',1)
} else {
this.$set(this,'age',null)
}
}
}
深度监听
data () {
return {
name1: '',
name2: '',
age: ''
}
},
computed: {
listenChange () {
const {name1,name2} = this
return {name1,name2}
}
},
watch: {
listenChang: {
handler(newVal,oldVal) {
if (val.name1 === val.name2) {
this.$set(this,'age',1)
} else {
this.$set(this,'age',null)
}
},
immediate: true,
deep: true
}
}