目录immediate 和 handlerdeep 深度监听以currentParams为例,监听selOrgId属性immediate 和 handler watch 的用法有个特
watch 的用法有个特点,就是当值第一次绑定的时候,不会执行监听函数,只有值发生改变才会执行。如果我们需要在最初绑定值得时候也执行函数,就需要用到 immediate 属性。
'currentParams.selOrgId': {
handler(newV, oldV) {
console.log("watch", newV, oldV)
},
immediate: true
}
当需要监听复杂的数据类型(对象)的改变时,普通的 watch 方法无法坚挺到对象内部属性的改变,此时就需要使用 deep 属性对对象进行深度监听。
currentParams: {
handler (newV, oldV) {
console.log('watch深度监听:', newV, oldV);
},
deep: true
}
watch深度监听 ,可以监听到对象中每个属性的变化,但是会给对象的所有属性都加上这个监听器,当对象属性较多时,每个属性值得变化都会执行 handler 。
data(){
return {
currentParams: {
selOrgId: '1'
},
}
}
1.通过input输入框触发watch
<input v-model="currentParams.selOrgId" />
data(){
return {
currentParams: {
selOrgId: '1',//声明不声明都可以触发watch
},
}
},
watch: {
'currentParams.selOrgId': (newV, oldV) => {
console.log("watch", newV, oldV)
},
},
2.通过js动态修改对象中属性值触发watch
data(){
return {
currentParams: {
},
}
},
watch: {
'currentParams.selOrgId': (newV, oldV) => {
console.log("watch", newV, oldV)
},
},
methods: {
triggerWatch () {
方法一、要监听的属性值(currentParams.selOrgId)不需要事先声明
this.currentParams=Object.assign({}, this.currentParams, {selOrgId:
"改变后的值"});
方法二、要监听的属性值(currentParams.selOrgId)不需要事先声明
this.$set(this.currentParams,'selOrgId',"改变后的值")
方法三、要监听的属性值(currentParams.selOrgId)需要事先声明,否则监听不到
this.currentParams.selOrgId = "改变后的值";
}
}
到此这篇关于Vue中watch监听对象中某个属性的文章就介绍到这了,更多相关vue watch监听对象属性内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: vue中watch监听对象中某个属性的方法
本文链接: https://www.lsjlt.com/news/211379.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0