[Vuejs]-How to increase the quantity according to the input given in Vue

2πŸ‘

βœ…

  1. Create itemsCount property on your component’s data object:

    data: function {
      return {
        itemsCount: ""
      }
    }
    
  2. Use v-model attribute to assign the value of your input to the itemsCount property.

    <input type="number" class="col-1 form-control" v-model="itemsCount" />

  3. Refactor addToCart() function to take itemsCount value into account.

    addToCart() {
      let quantity = this.itemsCount !== "" ? this.itemsCount : 1
      this.$store.dispatch('addProductToCart', {
        product: this.product,
        quantity: parseInt(quantity)
      })
    }
    
πŸ‘€user14967413

1πŸ‘

Have you considered something like this?

<input type="number" v-model="quantity" />

And later your method

addToCart() {
  this.$store.dispatch("addProductToCart", {
    product: this.product,
    quantity: this.quantity // this.quantity here
  });
}
πŸ‘€vch

Leave a comment