Vue购物车实例练习

Vue购物车实例练习_第1张图片

❤️功能介绍

  1. 金额 = 单价 * 数量金额会自动根据数量的变化进行变化,我们可以点击按钮增加或减少商品的数量。
  2. 合计金额:只有在序号列号勾选上才会被计入总金额中,金额总数会根据用户的操作自动更新数据。
  3. 删除:如图我们勾选了第2个商品,当我们点击删除时,只会删除被选中的产品。
  4. 全选:点击1次全选会选中所有商品,第2次点击时就会取消已经选中的商品。
  5. 添加:用户可以自己添加商品、商品的单价、数量

代码

表头



  序号
  商品
  单价(元)
  数量
  金额(元)

商品表

  1. 点击增加或减少会调用方法函数,改变商品的数量
  2. 商品数量采用input,使用v-model实时变化商品的数量,用户可以直接输入商品数量
  3. 金额会根据商品单价和数量自动进行变化


  
  
    图片
    {{value.name}}
    {{value.introduce}}  
  
  ¥{{ value.price }}
  
    
    
    
  
  
    
  ¥{{value.price*value.count}}

添加商品



  
  
  
  
  

全选、删除、合计等操作



  
    
全选
  删除
合计:

¥{{priceSums}}

数据源

data(){
    return{
      selectAllValue:1,
      priceSums:0,
      push:{
        name:null,
        price:null,
        count:null,
        prices:null,
      },
      list:[{
          checked:'false',
          name:'微信公众号',
          src:'https://mp.weixin.qq.com/mp/qrcode?scene=10000004&size=102&__biz=MzkzMjE5OTMwOA==&mid=2247487964&idx=1&sn=f7474b00c99bb7017530e4fbffd0c62d&send_time=',
          introduce:'扫码关注,和群友们一起交流学习',
          price:9999,
          count:2,
          prices:null,
      },
      {
          checked:'false',
          name:'QQ交流群',
          src:'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAGmlJREFUeF7tnet2pDgMhJP3f+jZ06GZNbTEVyWb3Ebza89ibFmqkkqGJu9vb29/3hb9+/PHn+r9/T1cfZwrGkNrjffQXKMBzrzRfSvWpf06a2T72ecg39D9iu/u2k/khwxPVYg/0OmjOlmNwBXd1gTZvOIAtQmy+exTCVIBd2bkSARidAaMFfbsdtAaRFLawxnglAicvTn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20MQmJogWgVz/URxHa83QRxvncY2QfIew8n0VH1XJxIn5LcTxDGGnKqUtqiEO02eI8EcgtBxIWl2AlFVYin7rdju7EfZ2z6GJJQSEwJ9tAatO/pfwdstP5hqgrxm7EgOKImGQELz0oFKE+Q1CqNPmiCCHCOQZVVByVARSei4VUlA+xiyvQlyTZD/AFIQv65aF8OXAAAAAElFTkSuQmCC',
          introduce:'',
          price:6666,
          count:1,
          prices:null,
      },
      { 
          checked:'false',
          name:'MacBookPro',
          src:'https://img10.360buyimg.com/n2/jfs/t1/203950/20/28591/140129/637ca996E10cf6f5c/bb31749afd714364.jpg',
          introduce:'',
          price:21488,
          count:1,
          prices:null,
      }],    
    }
  },

computed计算属性,根据金额列的变化而变化

计算属性是基于它们的响应式依赖进行缓存的,只在相关响应式依赖发生改变时它们才会重新求值。

computed:{
    // 计算总和
    priceSums:function(){
      var sum = 0;
      for(var i = 0; i != this.list.length; i++){
        if(this.list[i].checked == true){
          sum = sum + this.list[i].price * this.list[i].count;
        }   
      }
      return sum;
    }
  },

利用methods定义一些方法

methods:{
    //减少物品个数
    reduce:function(index){
      this.list[index].count = this.list[index].count - 1;
      if(this.list[index].count == -1){
        this.list.splice(index,1)
      }
    },
    
    // 增加物品个数
    add:function(index){
      this.list[index].count = this.list[index].count + 1;
    },
    
    // 添加新元素在购物车中
    pushValue:function(){    
      console.log(this.push.name)
      if(this.push.name == undefined) return 0;  
      for(var i=0; i

公众号:小知识酷,回复“VueShoppingCart230524”获取完整源码

你可能感兴趣的:(vue.js,前端,javascript,ecmascript,前端框架)