Vue實現簡單的購物車案例

本文實例為大傢分享瞭Vue實現簡單的購物車案例的具體代碼,供大傢參考,具體內容如下

代碼:

<template>
  <div>
    <div>
      <span>手機: </span>
      <span>價格</span> <input type="number" v-model.number="phonePrice">
      <span> 數量 </span><input type="number" v-model.number="phoneCount">
      <span> 小計: </span><span>{{phoneTotal}}元</span>
    </div>
    <div>
      <span>電腦: </span>
      <span>價格</span> <input type="number" v-model.number="computerPrice">
      <span> 數量 </span><input type="number" v-model.number="computerCount">
      <span> 小計: </span><span>{{computerTotal}}元</span>
    </div>
    <div>
      <span>運費: </span><input type="number" v-model.number="freight"><span>元</span><br>
      <span>共計: {{total}}元</span>
      <p>優惠: {{discounts}}元</p>
      <p>應付: {{allPrice}}</p>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      phonePrice: '', // 手機單價
      phoneCount: '', // 手機數量
      computerPrice: '', // 電腦單價
      computerCount: '', // 電腦數量
      freight: '', // 運費
      discounts: ''
    }
  },
  computed: {
    phoneTotal () {
      return this.phonePrice * this.phoneCount
    },
    computerTotal () {
      return this.computerPrice * this.computerCount
    },
    // 總價
    total () {
      return this.computerTotal + this.phoneTotal + this.freight
    },
    allPrice () {
      return this.total - this.discounts
    }
  },
  watch: {
    total: {
      depp: true,
      handler () {
        if (this.phoneTotal + this.computerTotal > 5000 && this.phoneTotal + this.computerTotal < 8000) {
          this.discounts = 100
        } else if (this.phoneTotal + this.computerTotal > 8000) {
          this.discounts = 200
        }
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: