更新同一对象的另一个属性后,如何自动更新数组中一个对象的属性?

时间:2019-12-05 15:26:00

标签: vue.js

我的模型如下:

export default {
   data() {
      return {
         ...
         salesOrder: {
            ...
            line_items: [{
               status: 'saved',
               item_number: 1,
               description: 'The first line item'
               cost: 458.00,
               quantity: 2
            }, {
               status: 'saved',
               item_number: 2,
               description: 'The second line item'
               cost: 0.589,
               quantity: 3
            }]
         }
      }
   }
}

它用于生成如下所示的UI: enter image description here

每当statusdescriptionquantity字段在同一订单项上发生更改时,我都希望更新cost字段,以使我的UI看起来像这样: enter image description here

问题是,看起来没有任何方法可以监视数组内对象的属性。

我可以为该数组创建一个计算属性,然后观察它,但是随后我得到整个数组作为valnewVal参数:

{
   computed: {
      lineItems() {
         return this.salesOrder.line_items;
      }
   },
   watch: {
      lineItems() {
         handler(val, newVal) {
            // val and newVal are the whole array
         },
         deep: true
      }
   }
}

有什么建议吗?预先感谢您的帮助。

0 个答案:

没有答案