vue watch数组变化问题

有个需求cate_list数字里面的对象比如quantity数量改变以及is_select数量改变需要watch可以触发并监到变化

    watch: {
        skuShow: 'get_item_sku',
        cart_list: {
            handler: function(newVal, oldVal) {
                console.log(newVal);
            },
            deep: true,
        }
    },
"cart_list": [{
            "id": "10",
            "item_id": "1408",
            "item_name": "\u840c\u716e miYmiY\u5b9d\u5b9d\u4e13\u5c5e\u8425\u517b\u7c73",
            "sku_name": "\u6708\u5ea6\u5305",
            "quantity": "2",
            "is_select": "1",
            "old_price": "0",
            "price": "99.00",
            "total": "198.00",
        }, {
            "id": "9",
            "item_id": "1408",
            "item_name": "\u840c\u716e miYmiY\u5b9d\u5b9d\u4e13\u5c5e\u8425\u517b\u7c73",
            "sku_name": "7\u5929\u8bd5\u5403\u5305",
            "quantity": "2",
            "is_select": "1",
            "old_price": "0",
            "price": "38.00",
            "total": "76.00",
        }]
阅读 22.6k
3 个回答

把数组转字符串存储,用的时候再解析为数组:

        watch: {
            test: {
                handler: function (newVal, oldVal) {
                    console.log('newVal', JSON.parse(newVal))
                    console.log('oldVal', JSON.parse(oldVal))
                },
                deep: true
            }
        },


        test1() {
            let arr = JSON.parse(this.test)
            arr[0].count=9
            this.test=JSON.stringify(arr)
        },

clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏