0

In Vue, I have a v-for loop that renders an input according to an object I have. The input is with a v-model to a computed function:

<template v-for="product in products>"
  {{ product.Name }}: <input type="text" v-model="productAmount">
</template>

and accordingly, the computed function with get and setter:

computed: {
  productAmount: {
    get () {
      this.product.DefaultAmount
    },
    set (newAmount) {
      // doing something
    }
  }
}

The problem is that this.product in get() is undefined.

Is there a way to refer to a dynamic variable of v-for inside the getter of the computed function?

Tzahi Leh
  • 2,002
  • 1
  • 15
  • 27
  • Possible duplicate of [VueJS How can I use computed property with v-for](https://stackoverflow.com/questions/40322404/vuejs-how-can-i-use-computed-property-with-v-for) – Traxo Oct 22 '18 at 06:30

1 Answers1

0

If you want to use a computed property you should create a child component and pass the product as a property (<Procuct v-for="p in products" :product="p"/>).

Alternatively if it is not an expensive operation you could easily replace the computed with a method instead though and use that:

...
methods: {
  getAmountForProduct(product) {
    return product.DefaultAmount
  }
}
TommyF
  • 6,660
  • 8
  • 37
  • 61