1. 程式人生 > 程式設計 >Vue實現簡單的購物車案例

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>

<scr
ipt> export default { data () { return { phonePrice: '',// 手機單價 phoneCount: '',// 手機數量 computerPrice: '',// 電腦單價 computerCount: '',// 電腦數量 freight: '',// 運費 discounts: '' } },computed: { phoneTotal () { return this.phonePricwww.cppcns.come * this.phoneCount },computerTotal () { return this.computerszXqMk
Price * 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.discouwww.cppcns.com
nts = 200 } } } } } </script> <style scoped lang='less'> </style>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。