Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

123
Vistas
Add only one object from my array to Cart in vue 3

I would like to add my object from my item array, only once to my cart. Right now you can add the same object several times. I have my array in a index.js file and my data comes from store (index.js). The language Im working with is Vue3 with router.

<div v-if="product" class="product-details">
       

            <h3 class="text-center">{{ product.name}}</h3>
            <p class="description">{{ product.description }}</p>
            <img :src ="` ${ product.img }`"/>
            <h3 class="text-center">{{ product.price.toFixed(2)}} Euro</h3>
    
            <div class="cart-total" v-if="product_total">
            <h3>In Cart</h3>
            <h4>{{ product_total}}</h4>
                
            </div>
             <div class="button-container"> 
                 <button class="add"  @click="addToCart()"> Add</button>
            </div>
        </div>
    </div>
</template>



<script>
        export default{
            data(){
                return{
                    
                }
            },

            props:['product', 'active'],
            methods: {
                addToCart() {
                    this.$store.commit('addToCart', this.product)
                }

                
            },
            computed: {
                    product_total(){
                        return this.$store.getters.productQuantity(this.product)
                    }
            }
        }

 mutations: {
    addToCart(state, product) {
      let item = state.cart.find(i => i.id === product.id)
      
      if (item){
          item.quantity++
      } else  {
        state.cart.push({...product, quantity: 1})
      }

      updateLocalStorage(state.cart)
    },


about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Don't add quantity like that. because if you add extra quantity than, vue js that propery is not reacted by default. so we have to tell vue , for reactivity, so vue can make getter and setter for that object.

mutations: {
        addToCart(state, product) {
          ...
          
          if (item){
            item.quantity++
          } else  {
            Vue.set(item,'quantity',1)   // now vue will add getters and setters for  this key  quantity . now quantity change will automatically reflect.
          }
    
        ...
        },

you can refer to this wonderful article. https://medium.com/js-dojo/reactivity-in-vue-js-and-its-pitfalls-de07a29c9407

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda