铁雪资源网 Design By www.gsvan.com

本文实例为大家分享了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('确定清空购物车吗"htmlcode">
<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,购物车

铁雪资源网 Design By www.gsvan.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
铁雪资源网 Design By www.gsvan.com

评论“vue实现购物车功能(商品分类)”

暂无vue实现购物车功能(商品分类)的评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?