這篇文章主要講解了vue實現(xiàn)購物車功能的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
成都創(chuàng)新互聯(lián)公司專注于行唐企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設公司,商城開發(fā)。行唐網(wǎng)站建設公司,為行唐等地區(qū)提供建站服務。全流程按需定制開發(fā),專業(yè)設計,全程項目跟蹤,成都創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務
new Vue({ el: "#app", data: { cIndex: 0, lists: [ { title: "推薦商品", goods: [ { id: 0, img: './images/goods.png', name: '散稱櫻桃1', price: '10.00', num: 0 }, { id: 1, img: './images/goods.png', name: '散稱櫻桃1', price: '10.00', num: 0 } ] }, { title: "推薦商品", goods: [ { id: 2, img: './images/goods.png', name: '散稱櫻桃2', price: '20.00', num: 0 }, { id: 3, img: './images/goods.png', name: '散稱櫻桃2', price: '20.00', num: 0 }, ] }, ], carArr: [], appearCar: false, }, computed:{ alltotal:function () { var alltotal = 0; var allNum = 0; for(var i = 0,len = this.lists.length;i<len;i++){ for(var j = 0,goodsLen = this.lists[i].goods.length;j<goodsLen;j++){ alltotal += parseInt(this.lists[i].goods[j].num)*parseFloat(this.lists[i].goods[j].price); allNum += parseInt(this.lists[i].goods[j].num); } } return [alltotal,allNum] }, }, methods: { leftTap: function (index) { this.cIndex = index }, showCar: function () { if (this.carArr.length == 0) { this.appearCar = false } else { this.appearCar = !this.appearCar } }, //商品數(shù)量加 addNum: function (data) { data.num++; var carArr = this.carArr; var isCar = false; if (carArr == 0) { carArr.push(data) } for (var i = 0, len = carArr.length; i < len; i++) { if (data.id == carArr[i].id) { isCar = true } } if (!isCar) { carArr.push(data) } }, reduceNum: function (data) { if (data.num <= 0) return; var carArr = this.carArr; data.num--; var isCar = false; for (var i = 0, len = carArr.length; i < len; i++) { if (carArr[i].num <= 0) { carArr.splice(i, 1) } } }, carAddNum: function (index) { this.carArr[index].num++ }, carReduceNum: function (index) { this.carArr[index].num--; if (this.carArr[index].num <= 0) { this.carArr.splice(index, 1) } if (this.carArr.length == 0) { this.appearCar = !this.appearCar } }, // 清空 delCar: function () { confirm('確定清空購物車嗎?'); var cur = this.cIndex; var goods = this.lists[cur].goods if (true) { this.carArr = []; this.appearCar = !this.appearCar; for(var i = 0,len = this.lists.length;i<len;i++){ for(var j = 0,goodsLen = this.lists[i].goods.length;j<goodsLen;j++){ this.lists[i].goods[j].num=0; } } } }, Settlement:function () { if(this.alltotal[0]<=0){ console.log('我不動') }else{ window.location.href = 'cashierPay.html' } } } })
html頁面
<div id="app" v-cloak> <div class='box'> <div class='boxleft'> <div class='boxla'> <img src='./images/sm.png'/> <span>掃碼</span> </div> <ul class='boxlbox'> <template v-for="(item,index) in lists"> <li @click='leftTap(index)' :class="{boxlb_cheak:cIndex==index}" class='boxlb'> <span class='boxlb_inner' :class="{boxlb_inner_cheak:cIndex==index}">{{item.title}}</span> </li> </template> </ul> </div> <ul class='boxright'> <li class='boxri' v-for="(item,index) in lists[cIndex].goods"> <img class='boxri_img' :src='item.img'/> <div class='boxri_text'> <div class='boxrit_left'> <p class='boxritl_name'>{{item.name}}</p> <p class='boxritl_price'>¥{{item.price}}</p> </div> <div class='boxrit_right'> <img @click='reduceNum(item)' v-show="item.num>0" class='boxrit_right_img' src='./images/minus.png'/> <span class='boxrit_right_num' v-show="item.num>0">{{item.num}}</span> <img @click='addNum(item)' class='boxrit_right_img' src='./images/add.png'/> </div> </div> </li> </ul> </div> <footer class='footer'> <div class='cartImgBox'> <img class='cartImg' src='./images/car.png' @click='showCar'/> 共{{alltotal[1]}}件商品 </div> <div class='footerb'>¥{{alltotal[0]}}</div> <div @click='Settlement' class='footerc'>結算</div> </footer> <!-- 購物車 --> <div class='shop-car-mask' v-show="appearCar"> <div class='shop-com'> <div class='shop-title'> <span>已選商品</span> <span @click="delCar">清空</span> </div> <ul class="car-shopList"> <template v-for='(item,index) in carArr'> <li class='car-list'> <div class='car-img'> <img :src='item.img'/> </div> <div class='car-name'>{{item.name}}</div> <div class='car-num'>¥{{item.price}}</div> <div class='boxrit_right2'> <img @click='carReduceNum(index)' class='boxrit_right_img2' src='./images/minus.png'/> <span class='boxrit_right_num2'>{{item.num}}</span> <img @click='carAddNum(index)' class='boxrit_right_img2' src='./images/add.png'/> </div> </li> </template> </ul> </div> </div> </div>
看完上述內容,是不是對vue實現(xiàn)購物車功能的方法有進一步的了解,如果還想學習更多內容,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當前題目:vue實現(xiàn)購物車功能的方法
當前路徑:http://m.newbst.com/article18/ggpsgp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、響應式網(wǎng)站、ChatGPT、靜態(tài)網(wǎng)站、移動網(wǎng)站建設、虛擬主機
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)