Vue组件简易模拟实现购物车


Posted in Vue.js onDecember 21, 2020

本文实例为大家分享了Vue组件模拟实现购物车的具体代码,供大家参考,具体内容如下

Vue组件简易模拟实现购物车

代码:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Document</title>
 <script src="./lib/vue-2.4.0.js"></script>
 <style>
  #app{
   width:600px;
  }

  #myTable{
   width:500px;
   border-collapse:collapse;
  }

  td, th{
   text-align: center;
   font-size:20px;
   border:2px solid black;
  }

  td{
   height: 40px;
  }

  input{
   width: 30px;
   text-align: center   
  }

 </style>
</head>
<body>

 <div id="app">
  <my-cart></my-cart>
 </div>

 <script>
  var MyCommmodity = {
   props: ["list"],
   template:`
    <div>
     <button @click="baicai">白菜</button>
     <button @click="qingcai">青菜</button>
     <button @click="luobo">萝卜</button> 
    </div>
   `,
   methods: {
    baicai: function(){
     var cai = {};
     cai.id = 4;
     cai.name = "白菜"
     cai.price = 3;
     cai.num = 1;
     this.list.push(cai)
    },
    qingcai: function(){
     var cai = {};
     cai.id = 5;
     cai.name = "青菜"
     cai.price = 6;
     cai.num = 1;
     this.list.push(cai)
    },
    luobo: function(){
     var cai = {};
     cai.id = 6;
     cai.name = "萝卜"
     cai.price = 8;
     cai.num = 1;
     this.list.push(cai)
    }
   }
  }

  var MyTable = {
   props: ["list", "flag"],
   template:`
    <table id="myTable">
    <tr>
     <th>编号</th>
     <th>名称</th>
     <th>单价</th>
     <th>数量</th>
     <th>操作</th>
    </tr>
    <tr :key="item.id" v-for="item in list"> 
     <td>{{item.id}}</td>
     <td>{{item.name}}</td>
     <td>{{item.price}}</td>
     <td>
      <button :disabled="flag" @click="sub(item.id)">-</button>
      <input type="text" :value="item.num" @blur="changeNum(item.id,$event)">
      <button @click="add(item.id)">+</button>
     </td>
     <td>
      <button @click="del(item.id)">删除</button>
     </td>
    </tr>
   </table>
   `,
   methods: {
    changeNum: function(id, event){
     this.$emit("change-num",{
      id: id,
      type: "change",
      num: event.target.value
     });
    },
    sub: function(id){
     this.$emit("change-num",{
      id: id,
      type: "sub"
     })
    },
    add: function(id){
     this.$emit("change-num",{
      id: id,
      type: "add"
     })
    },    
    del: function(id){
     // alert(id);
     this.$emit("del-cart",id)
    }
   }
  }

  var MyPrice = {
   props: ["list"],
   template:`
    <div>
     <span>结算:</span>
     <span>{{total}}</span>     
    </div>
   `,
   computed: {
    total: function(){
     var t = 0;
     this.list.forEach(item => {
      t += item.price * item.num;
     });
     return t;
    }
   }
  }

  Vue.component('my-cart', {
   data () {
    return {
     flag:false,
     list:[{
      id: 1,
      name: "猪",
      price: "10",
      num:1,
     },
     {
      id: 2,
      name: "牛",
      price: "11",
      num:1,
     },
     {
      id: 3,
      name: "鸡",
      price: "13",
      num:1,
     }]
    }
   },
   template:`
    <div> 
     <my-commmodity :list="list"></my-commmodity> 
     <my-table :list="list" :flag="flag" @change-num="changeNum($event)" @del-cart="delCart($event)"></my-table>
     <my-price :list="list"></my-price>
        
    </div>
   `,
   components:{ 
    'my-table':MyTable,
    'my-price':MyPrice,
    'my-commmodity':MyCommmodity,
   },
   methods:{
    changeNum: function(val){
     if(val.type ==="change"){
      this.list.some(item=>{
       if(item.id == val.id){
        item.num = val.num;
        return true;
       }
      });      
     }else if(val.type ==="sub"){
      this.list.some(item=>{
       if(item.id == val.id && item.num >0){
        item.num -= 1;
        return true;
       }
      }); 
     }else if(val.type ==="add"){
      this.list.some(item=>{
       if(item.id == val.id){
        item.num += 1;
        return true;
       }
      }); 
     }

    },
    delCart: function(id){
     var index = this.list.findIndex(item=>{
      return item.id == id;
     })
     this.list.splice(index,1)
    }
   }
  })


 var vm = new Vue({
  el: '#app',
  data:{
  }
 })
 </script>
</body>
</html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Vue.js 相关文章推荐
在vue中通过render函数给子组件设置ref操作
Nov 17 Vue.js
详解vue中使用transition和animation的实例代码
Dec 12 Vue.js
vue的hash值原理也是table切换实例代码
Dec 14 Vue.js
Vue组件简易模拟实现购物车
Dec 21 Vue.js
vue 实现基础组件的自动化全局注册
Dec 25 Vue.js
vue 动态生成拓扑图的示例
Jan 03 Vue.js
vue+element table表格实现动态列筛选的示例代码
Jan 14 Vue.js
如何在Vue项目中添加接口监听遮罩
Jan 25 Vue.js
Vue 3自定义指令开发的相关总结
Jan 29 Vue.js
详解Vue的七种传值方式
Feb 08 Vue.js
详解vue3中组件的非兼容变更
Mar 03 Vue.js
vue-cli3.0修改打包后的文件名和文件地址,打包后本地运行报错解决
Apr 06 Vue.js
vue实现购物车的小练习
Dec 21 #Vue.js
Vue实现小购物车功能
Dec 21 #Vue.js
vue监听滚动事件的方法
Dec 21 #Vue.js
vue el-upload上传文件的示例代码
Dec 21 #Vue.js
vue 在单页面应用里使用二级套嵌路由
Dec 19 #Vue.js
vue中如何添加百度统计代码
Dec 19 #Vue.js
vue 导航守卫和axios拦截器有哪些区别
Dec 19 #Vue.js
You might like
优化NFR之一 --MSSQL Hello Buffer Overflow
2006/10/09 PHP
PHP新手上路(五)
2006/10/09 PHP
ThinkPHP CURD方法之page方法详解
2014/06/18 PHP
laravel 关联关系遍历数组的例子
2019/10/10 PHP
js玩一玩WSH吧
2007/02/23 Javascript
动态加载script文件的两种方法
2013/08/15 Javascript
使用plupload自定义参数实现多文件上传
2016/07/19 Javascript
浅谈js中test()函数在正则中的使用
2016/08/19 Javascript
JavaScript 计算笛卡尔积实例详解
2016/12/02 Javascript
node.js请求HTTPS报错:UNABLE_TO_VERIFY_LEAF_SIGNATURE\的解决方法
2016/12/18 Javascript
JS/CSS实现字符串单词首字母大写功能
2019/09/03 Javascript
在elementui中Notification组件添加点击事件实例
2020/11/11 Javascript
python实现中文分词FMM算法实例
2015/07/10 Python
深入解析Python中的descriptor描述器的作用及用法
2016/06/27 Python
在Python中通过threading模块定义和调用线程的方法
2016/07/12 Python
利用Anaconda简单安装scrapy框架的方法
2018/06/13 Python
Django框架多表查询实例分析
2018/07/04 Python
python mac下安装虚拟环境的图文教程
2019/04/12 Python
python导入坐标点的具体操作
2019/05/10 Python
python模拟菜刀反弹shell绕过限制【推荐】
2019/06/25 Python
wxPython电子表格功能wx.grid实例教程
2019/11/19 Python
python json 递归打印所有json子节点信息的例子
2020/02/27 Python
利用Python的folium包绘制城市道路图的实现示例
2020/08/24 Python
Python爬虫自动化爬取b站实时弹幕实例方法
2021/01/26 Python
html5 datalist 选中option选项后的触发事件
2020/03/05 HTML / CSS
MyFrenchPharma中文网:最大的法国药妆平台
2016/10/07 全球购物
英国经济型酒店品牌:Travelodge
2019/12/17 全球购物
Linux的主要特性
2014/10/06 面试题
内科护士实习自我鉴定
2013/10/17 职场文书
会计应聘求职信范文
2013/12/17 职场文书
失职检讨书大全
2015/01/26 职场文书
优秀大学生自荐信
2015/03/26 职场文书
导游词之昭君岛
2020/01/17 职场文书
vue3中的组件间通信
2021/03/31 Vue.js
解决Python字典查找报Keyerror的问题
2021/05/26 Python
使用feign服务调用添加Header参数
2021/06/23 Java/Android