vue watch 数组对象中用法

[{count:1,price:2},{count:2,price:3}] 如何通过监听里面count,price,每个对象中增加赋值totalprice字段

<template>
  <div>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        Count: {{ item.count }}, Price: {{ item.price }}, Total Price: {{ item.totalPrice }}
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      items: [
        { count: 1, price: 2 },
        { count: 2, price: 3 },
      ],
    };
  },
  watch: {
    items: {
      deep: true,
      immediat:true,
      handler(newVal, oldVal) {
        for (let i = 0; i < newVal.length; i++) {
          const item = newVal[i];
          item.totalPrice = item.count * item.price;
        }
      },
    },
  },
};
</script>

监听对象中指定某元素

  watch:{
      'form.eiScale':function(newVal){
        console.log('newVal---',newVal);
        this.jingweiVisible = newVal == '规上' ? true : false;
      }
 },
posted @ 2023-08-14 14:57  盘思动  阅读(57)  评论(0编辑  收藏  举报