扫码关注官方订阅号
如图我要监听items.amount,除了for循环的写法,还有什么更简单的写法吗?
computed: { totalAmount () { // 计算出 items 数组中的 amount 总额 return this.items.reduce((a, b) => ({ amount: a.amount + b.amount })).amount } }, watch: { totalAmount (newVal) { // 当计算属性变更时触发更新 console.log('amount change to ', newVal) } }
个人觉得,你的总额的money应该改成一个计算属性
computed: { money() { let sum = 0; this.items.forEach(item => { sum += item.amount; }); return sum; } }
然后将money属性从data中删除,同时删除你的watch
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
个人觉得,你的总额的money应该改成一个计算属性
然后将money属性从data中删除,同时删除你的watch