Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
Adding item to cart and removing from cart

I have a + button to add item to cart,it works. But at the same time when number increments in cart I need number that says in stock will decrement number in stock that displays it doesn't work …

I need to do the same with - button

I really need the displayed inStock number to change when I add or remove items from cart Can someone please help me? I'm new at this

Vue.component('product', {

    template: 
    `
     <div class="product">
          
        <div class="product-image">
          <img :src="image" />
        </div>
  
        <div class="product-info">
            <h1>{{ product }}</h1>
            <p v-if="inStock">In Stock {{inStock}}</p>
            <p v-else>Out of Stock</p>
  
            <button @click="addToCart" 
              :disabled="!inStock"
              :class="{ disabledButton: !inStock }"> + </button>

            <button @click="removeFromCart"> - </button>
  
         </div>  
      
      </div>
     `,
    data() {
      return {
          product: 'Hoodie',
          image: 'hoodie.jpeg',
          inStock: 10,
          cart: 0
      }
    },
      methods: {
        addToCart: function() {
            this.$emit('add-to-cart')
        },
        removeFromCart: function() {
             this.$emit('remove-from-cart')
        }
      }
  })
  
  var app = new Vue({
      el: '#app',
      data: {
        cart: 0,
        inStock: 10
      },
      methods: {
        updateCart() {
          this.cart  += 1,
          this.inStock -= 1,
        },
        removeItem() {
          this.cart  -= 1,
          this.inStock += 1
        }
      }
  })

HTML:

<div class="nav-bar"></div>

            <div id="app">
            <div class="cart">
                <p>Cart({{ cart }})</p>
            </div>
            <product @add-to-cart="updateCart" @remove-from-cart="removeItem"></product>
            </div> 
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Inside your component, remove the data attributes inStock and cart and props section.

Vue.component('product', {

    template: 
    `
     <div class="product">
          
        <div class="product-image">
          <img :src="image" />
        </div>
  
        <div class="product-info">
            <h1>{{ product }}</h1>
            <p v-if="inStock">In Stock {{inStock}}</p>
            <p v-else>Out of Stock</p>
  
            <button @click="addToCart" 
              :disabled="!inStock"
              :class="{ disabledButton: !inStock }"> + </button>

            <button @click="removeFromCart"> - </button>
  
         </div>  
      
      </div>
     `,
    data() {
      return {
          product: 'Hoodie',
          image: 'hoodie.jpeg',
      }
    },
    props: {
      inStock: {
        type: Number,
        default: 0
      },
      cart : {
        type: Number,
        default: 0
      }
    },
      methods: {
        addToCart: function() {
            this.$emit('add-to-cart')
        },
        removeFromCart: function() {
             this.$emit('remove-from-cart')
        }
      }
  })
  
  var app = new Vue({
      el: '#app',
      data: {
        cart: 0,
        inStock: 10
      },
      methods: {
        updateCart() {
          this.cart  += 1,
          this.inStock -= 1,
        },
        removeItem() {
          this.cart  -= 1,
          this.inStock += 1
        }
      }
  })

Then in your parent component, pass the props.

Parent Component (HTML)

<div class="nav-bar"></div>

            <div id="app">
            <div class="cart">
                <p>Cart({{ cart }})</p>
            </div>
            <!-- changes for props added below -->
            <product :cart="cart" :inStock="inStock" @add-to-cart="updateCart" @remove-from-cart="removeItem"></product>
            </div>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!