Vue中watch怎么实现监听对象,基本用法是什么
Admin 2022-06-14 群英技术资讯 692 次浏览
watch的作用:监听vue实例上数据的变动
示例:
queryData: { name: '', creator: '', selectedStatus: '', time: [], },
1、普通的watch
data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValue) { console.log(newValue) } }
2、数组的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中的属性发生变化(可被监测到的),便会执行handler函数;
如果想监测具体的属性变化,如pokerHistory变化时,才执行handler函数,则可以利用计算属性computed做中间层。
事例如下:
4、对象具体属性的watch[活用computed]
data() { return { bet: { pokerState: 53, pokerHistory: 'local' } } }, computed: { pokerHistory() { return this.bet.pokerHistory } }, watch: { pokerHistory(newValue, oldValue) { console.log(newValue) } }
总结
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
基于JQuery实现页面定时弹出广告的代码如下
jQuery是一bai个简洁而快速的JavaScript库,可用于du简化zhi事件处理,HTML文档遍历,Ajax交互和dao动画,以更快速开发网站
本文详细讲解了Javascript的作用域、作用域链以及闭包,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
去除HTML中没有ID元素的方法:1、使用“$("父元素").children(":not([id])")”语句获取到需要去除的所有没有ID的元素;2、使用remove()方法删除获取到的元素即可,语法“没有ID的元素.remove()”。
今天给大家分享的是关于JavaScript中链式调用的内容,那么JavaScript中链式调用是什么意思?下面就给大家来详细的介绍一下链式调用以及方法,感兴趣的朋友接下来跟随小编一起看看吧。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008