javascript - vue listens to an item in an array in data
女神的闺蜜爱上我
女神的闺蜜爱上我 2017-07-05 10:59:38
0
2
741

As shown in the figure, I want to monitor items.amount. In addition to the for loop, is there any simpler way to write it?

女神的闺蜜爱上我
女神的闺蜜爱上我

reply all(2)
女神的闺蜜爱上我
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)
  }
}
刘奇

Personally, I think your total money should be changed to a calculated attribute

computed: {
         money() {
              let sum  = 0;
              this.items.forEach(item => {
                     sum += item.amount;
              });
              return sum;
         }
}

Then delete the money attribute from data and delete your watch

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!