1

I'm new with Vue.js and i'm using a v-for to iterate an array of objects, but i need to calculate a single field (precoPorKg) in this v-for.

to calculate this i need that input item.quantidade had some value and after this, i need to make this operation:

(item.quantidade * item.valorUnitario)/( item.quantidade * item.pesoUnitario)

My v-for:

 <tr v-for="(item, index) in step2">
     <td>{{ item.produto }}</td>
     <td><input type="text" v-model="item.quantidade"></td>
     <td>{{ item.valorUnitario }}</td>
     <td>{{ item.pesoUnitario }}</td>
     <td>{{ item.cubagemUnitaria }}</td>
     <td>{{ calcPrecoPorKg(index, item) }}</td> 
 </tr>

my vue component (i'm trying to make this operation in computed, but doesn't work):

   new Vue({
                el: '#app', 
                data(){
                step2 : [
                  {
                    produto: 'A70-5',
                    descricao: 'bola de basquete',
                    ncm: '950.6',
                    quantidade: '',
                    valorUnitario: '0,78',
                    pesoUnitario: '0,430',
                    cubagemUnitaria: '0,00',
                    precoPorKg: '',
                  }
             ],
         computed: {
            calcPrecoPorKg: function (index, item) {
                return (item.quantidade * item.valorUnitario)/( item.quantidade * item.pesoUnitario);
                     }
                }

    })
2
  • Try making this a method rather than computed property and it should work Commented May 17, 2020 at 0:35
  • @Himanshu how can i do this? Commented May 17, 2020 at 0:35

1 Answer 1

2

computed properties do not take any parameters. What you need is just a method.

methods: {
  calcPrecoPorKg(item) {
    return (item.quantidade * item.valorUnitario)/( item.quantidade * item.pesoUnitario);
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you, i changed to method and i was using wrong values to the operation, i correct this and it works! thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.