Home > Back-end >  how to stop the count from going negative when the value is zero?
how to stop the count from going negative when the value is zero?

Time:01-24

I'm trying to create an add to card button but I'm stuck on the count going negative,basically when we click on remove item at value zero its goes negative(-1) since I have just started learning vuejs and this is totally new to me so any help would be apreciated!

<script>
export default {
  data() {
    return {
      count: 0
    }
  },
  methods: {
    increment() {
      this.count  
    },
    decrement(){
      this.count--
    }
  },
  mounted() {
    this.increment()
    // this. decrement()
  }
}
</script>

<template>
  <div >
    <!-- <button > count  is : {{ count }}</button><br> -->
    <button   @click="increment">add to cart</button>
    <button   @click="decrement">remove item</button>
  </div>
  <h1 v-if= count> item added {{count}} </h1>
    <h1 v-else-if = count> please add item </h1>
  <h1 v-else> no item            
  • Related