How to calculate two input values and put the result in another input field in Vue,js?

后端 未结 4 453
执念已碎
执念已碎 2021-01-21 04:19

Here are my input fields:



        
4条回答
  •  -上瘾入骨i
    2021-01-21 04:57

    Although there already is an accepted answer and it works this is the perfect use case for computed property and it should be used instead of methods.

    Below is the working example.

    new Vue({
      el: "#el",
      data() {
        return {
          form: {
            sale_quantity: 0,
            sale_rate: 0,
            sale_total: 0
          }
        }
      },
      computed: {
        total: function() {
          let calculatedTotal = this.form.sale_quantity * this.form.sale_rate;
          this.sale_total = calculatedTotal;
          
          return calculatedTotal;
        }
      }
    })
    
    

    Total: {{sale_total}}

提交回复
热议问题