这篇文章主要讲解了vue实现购物车功能的方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。
成都创新互联公司专注于行唐企业网站建设,成都响应式网站建设公司,商城开发。行唐网站建设公司,为行唐等地区提供建站服务。全流程按需定制开发,专业设计,全程项目跟踪,成都创新互联公司专业和态度为您提供的服务
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 } }, //商品数量加 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实现购物车功能的方法有进一步的了解,如果还想学习更多内容,欢迎关注创新互联行业资讯频道。
当前题目:vue实现购物车功能的方法
当前路径:https://www.cdcxhl.com/article18/ggpsgp.html
成都网站建设公司_创新互联,为您提供网站收录、响应式网站、ChatGPT、静态网站、移动网站建设、虚拟主机
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联