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 @   盘思动  阅读(72)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示