vue实现购物车案例


Posted in Javascript onMay 30, 2020

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

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>购物车案例</title>
 <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<style>
 *{
 padding: 0;
 margin:0
 }
 ul li{
 width: 1200px;
 display: flex;
 align-items: center;
 justify-content: center;
 }
 li div,.total{
 display: inline-block;
 width:200px;
 height: 50px;
 line-height: 50px;
 text-align: center;
 }
 button{
 width: 60px;
 height: 40px;
 text-align: center;
 line-height: 40px;
 }

</style>
<body>

 <div id="app">
 <ul>
  <goodsitem 
  v-for="item in goodslist" 
  :item="item" 
  :key="item.id"
  @onchange="(type)=>{handleCount(type,item)}"
  @ondelete="()=>{handleDelete(item.id)}">
  </goodsitem>
  <div class="total" style="padding-left: 113px">总价:{{total}}</div>
 </ul>
 </div>

</body>
<script>
 var computed={
 props:{
  count:{
   type:Number,
   require:true
  }
 },
 methods:{
  handleCount(type){
  this.$emit('onchange',type)
  }
 },
 template:`<div style="width:200px">
   <button @click="handleCount('sub')">-</button>
   <span>{{count}}</span>
   <button @click="handleCount('add')" >+</button>
   </div>
 
 `
 
 }
 var goodsitem={
 props:{
  item:{
  type:Object,
  require:true
  }
 },
 methods:{
  handleCount(type){
  this.$emit('onchange',type)
  },
  handleDelete(){
  this.$emit('ondelete')
  }
 },
 components:{
  computed
 },
 template:`<li>
   <div>{{item.goodsName}}</div>
   <div>{{item.price}}</div>
   <computed :count="item.count" @onchange="handleCount"></computed>
   <div>{{item.sum}}</div>
   <div><button @click="handleDelete">删除</button></div>
   </li>
   `
 }

 var app=new Vue({
 el:"#app",
 data:{
  goodslist:[{
  id:1,
  goodsName:"小可爱",
  price:100,
  count:1,
  sum:100
  },{
  id:2,
  goodsName:"小可爱",
  price:200,
  count:2,
  sum:400
  },{
  id:3,
  goodsName:"小可爱",
  price:300,
  count:3,
  sum:900
  },{
  id:4,
  goodsName:"小可爱",
  price:400,
  count:1,
  sum:400
  },
  ]
 },
 methods:{
  handleCount(type,item){
  if(type=='add'){
   item.count+=1
  }else{
   if(item.count==1){
   this.handleDelete(item.id) 
   return
   }
   item.count-=1
  }
  item.sum=item.count*item.price
  },
  handleDelete(id){
  return this.goodslist=this.goodslist.filter((item)=>{
   return id!=item.id
  })
  }
 },
 computed:{
  total(){
  return this.goodslist.reduce((total,item)=>{
   return total+=item.sum
  },0)
  }
 },
 components:{
  goodsitem
 }
 })
</script>
</html>

实现效果图:

vue实现购物车案例

小编再为大家分享一段收藏的vue购物车逻辑代码,也谢谢原作者的分享

<template>
 <div class="hello">
 <div class="main">
 <div v-if="hasList">
  <div class="cart-box">
   <div class="cart-list" v-for="(item,index) in carts" :key="index">
    <!-- <icon v-if="item.selected" type="success" color="red" data-index="index" class="cart-pro-select" @click="selectList"/>
    <icon v-else type="circle" class="cart-pro-select" data-index="index" @click="selectList"/> -->
    <router-link to="/"><img class="cart-thumb" :src="item.image" /></router-link>
    <p class="cart-pro-name">{{item.title}}</p>
    <p class="cart-pro-price">¥{{item.price}}</p>
    <div class="cart-count-box">
     <p class="cart-count-down" @click="minusCount(item.id)" data-obj="obj" data-index="index">-</p>
     <p class="cart-count-num">{{item.num}}</p>
     <p class="cart-count-add" @click="addCount(item.id)" data-index="index">+</p>
    </div>
    <p class="cart-del" @click="deleteList" data-index="index">×</p>
   </div>
  </div>
 
  <div class="cart-footer">
   <!-- <icon v-if="selectAllStatus" type="success_circle" color="#fff" class="total-select" @click="selectAll"/>
   <icon v-else type="circle" color="#fff" class="total-select" @click="selectAll"/> -->
   <div class="order-icon">
    <router-link to="/"><img src="../../static/image/icon3.png" /></router-link>
   </div>
   <p>全选</p>
   <p class="cart-toatl-price">¥{{totalPrice}}</p>
  </div>
 </div>
 <div v-else>
  <div class="cart-no-data">购物车是空的哦~</div>
 </div>
</div>
 </div>
</template>
<script>
 
// import {Toast} from 'vant'
export default {
 data () {
 return {
  carts: [{id: 1, title: '新鲜芹菜 半斤', image: '../../static/image/s5.png', num: 4, price: 0.01, selected: true},
  {id: 2, title: '素米 500g', image: '../../static/image/s6.png', num: 1, price: 0.03, selected: true}],
  hasList: true,
  totalPrice: 0,
  selectAllStatus: true,
  obj: {
  name: 'hello'
  }
 }
 },
 created () {
 // this.$set({
 // hasList: true,
 // carts: [
 //  {id: 1, title: '新鲜芹菜 半斤', image: '../../static/image/s5.png', num: 4, price: 0.01, selected: true},
 //  {id: 2, title: '素米 500g', image: '../../static/image/s6.png', num: 1, price: 0.03, selected: true}
 // ]
 // })
 this.getTotalPrice()
 },
 methods: {
 selectList (e) {
  console.log(e)
  let carts = this.carts
  let index = 1
  const selected = carts[index].selected
  carts[index].selected = !selected
  this.setData({
  carts: carts
  })
  this.getTotalPrice()
 },
 deleteList (e) {
  const index = e.currentTarget.dataset.index
  let carts = this.carts
  carts.splice(index, 1)
  this.$set({
  carts: carts
  })
  if (!carts.length) {
  this.$set({
   hasList: false
  })
  } else {
  this.getTotalPrice()
  }
 },
 selectAll (e) {
  let selectAllStatus = this.data.selectAllStatus
  selectAllStatus = !selectAllStatus
  let carts = this.carts
 
  for (let i = 0; i < carts.length; i++) {
  carts[i].selected = selectAllStatus
  }
  this.$set({
  selectAllStatus: selectAllStatus,
  carts: carts
  })
  this.getTotalPrice()
 },
 addCount (e) {
  let carts = this.carts
  let num = carts[e - 1].num
  num = num + 1
  carts[e - 1].num = num
  this.$set(this.carts, carts)
  this.getTotalPrice()
 },
 minusCount (e) {
  // const obj = e.currentTarget.dataset.obj
  let carts = this.carts
  let num = carts[e - 1].num
  if (num <= 1) {
  return false
  }
  num = num - 1
  carts[e - 1].num = num
  this.$set(this.carts, carts)
  this.getTotalPrice()
 },
 getTotalPrice () {
  let carts = this.carts
  let total = 0
  for (let i = 0; i < carts.length; i++) {
  if (carts[i].selected) {
   total += carts[i].num * carts[i].price
  }
  }
  this.$set({
  carts: carts,
  totalPrice: total.toFixed(2)
  })
 }
 }
}
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<!-- scoped css样式仅仅在当前模板使用-->
<style scoped>
.cart-box{
 padding-bottom: 50px;
}
.cart-list{
 position: relative;
 padding: 10px 10px 10px 142px;
 height: 92px;
 border-bottom: 1px solid #e9e9e9;
}
.cart-list .cart-pro-select{
 position: absolute;
 left: 10px;
 top: 45px;
 width: 25px;
 height: 25px;
}
 
.cart-list .cart-thumb{
 position: absolute;
 top: 10px;
 left: 42px;
 width: 92px;
 height: 92px;
}
.cart-list .cart-pro-name{
 display: inline-block;
 width: 150px;
 height: 52px;
 line-height: 25px;
 overflow: hidden;
}
.cart-list .cart-pro-price{
 display: inline-block;
 float: right;
 height: 52px;
 line-height: 25px;
}
.cart-list .cart-count-box{
 position: absolute;
 left: 142px;
 bottom: 10px;
 width: 125px;
 height: 40px;
}
.cart-list .cart-count-box p{
 display: inline-block;
 line-height: 40px;
 p-align: center;
}
.cart-count-down,.cart-count-add{
 position: absolute;
 top:-5px;
 font-size: 22px;
 width: 25px;
 height: 100%;
}
.cart-count-num{
 margin-left: 17px;
 width: 75px;
}
.cart-del{
 position: absolute;
 right: 10px;
 bottom: 0px;
 width: 40px;
 height: 40px;
 line-height: 40px;
 p-align: center;
 font-size: 22px;
}
.cart-footer{
 position: fixed;
 bottom: 15%;
 left: 0;
 width: 100%;
 height: 45px;
 line-height: 45px;
 box-sizing: border-box;
 background: #AB956D;
 color: #fff;
}
.total-select{
 position: absolute;
 left: 10px;
 top: 12px;
 width: 22px;
 height: 22px;
}
.order-icon{
 position: absolute;
 right: 20px;
 top: 12px;
 width: 24px;
 height: 24px;
}
.order-icon image,.order-icon navigator{
 display: block;
 width: 24px;
 height: 24px;
}
.cart-toatl-price{
 float: right;
 width: 60px;
}
 
.cart-no-data{
 padding:20px 0;
 color: #999;
 p-align: center;
}
</style>

更多文章可以点击《Vue.js前端组件学习教程》学习阅读。

关于vue.js组件的教程,请大家点击专题vue.js组件学习教程进行学习。

更多vue学习教程请阅读专题《vue实战教程》

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

Javascript 相关文章推荐
JS中动态添加事件(绑定事件)的代码
Jan 09 Javascript
javascript引擎长时间独占线程造成卡顿的解决方案
Dec 03 Javascript
常用的JS验证和函数汇总
Dec 23 Javascript
JavaScript使用indexOf获得子字符串在字符串中位置的方法
Apr 06 Javascript
跟我学习javascript的call(),apply(),bind()与回调
Nov 16 Javascript
原生javascript实现分享到朋友圈功能 支持ios和android
May 11 Javascript
javascript三种代码注释方法
Jun 02 Javascript
在Vue组件化中利用axios处理ajax请求的使用方法
Aug 25 Javascript
轻量级JS Cookie插件js-cookie的使用方法
Mar 22 Javascript
jQuery实现数字自动增加或者减少的动画效果示例
Dec 11 jQuery
Javascript Dom元素获取和添加详解
Sep 24 Javascript
详解vue实现坐标拾取器功能示例
Nov 18 Vue.js
vue.js实现简单购物车功能
May 30 #Javascript
vue实现简单学生信息管理
May 30 #Javascript
vue实现学生信息管理系统
May 30 #Javascript
vue实现在线学生录入系统
May 30 #Javascript
VueJS实现用户管理系统
May 29 #Javascript
如何在vue中使用jointjs过程解析
May 29 #Javascript
vue实现信息管理系统
May 30 #Javascript
You might like
蝙蝠侠:侠影之谜
2020/03/04 欧美动漫
基于jquery1.4.2的仿flash超炫焦点图播放效果
2010/04/20 Javascript
jQuery 通过事件委派一次绑定多种事件,以减少事件冗余
2010/06/30 Javascript
快速排序 php与javascript的不同之处
2011/02/22 Javascript
浅析JavaScript中的常用算法与函数
2013/11/21 Javascript
JavaScript sup方法入门实例(把字符串显示为上标)
2014/10/20 Javascript
AngularJS的内置过滤器详解
2015/05/14 Javascript
AngularJS基础教程之简单介绍
2015/09/27 Javascript
js老生常谈之this,constructor ,prototype全面解析
2016/04/05 Javascript
js停止冒泡和阻止浏览器默认行为的简单方法
2016/05/15 Javascript
JS弹性运动实现方法分析
2016/12/15 Javascript
整理一些最近经常遇到的前端面试题
2017/04/25 Javascript
vue.js父组件使用外部对象的方法示例
2017/04/25 Javascript
详解Vue用axios发送post请求自动set cookie
2017/05/10 Javascript
分享ES6的7个实用技巧
2018/01/18 Javascript
webpack之devtool详解
2018/02/10 Javascript
Vue波纹按钮组件制作
2018/04/30 Javascript
详解vue2.0 资源文件assets和static的区别
2018/11/27 Javascript
vue中实现弹出层动画效果的示例代码
2020/09/25 Javascript
vue实现移动端返回顶部
2020/10/12 Javascript
JS+CSS实现动态时钟
2021/02/19 Javascript
[41:05]Serenity vs Pain 2018国际邀请赛小组赛BO2 第二场 8.19
2018/08/21 DOTA
python元组操作实例解析
2014/09/23 Python
合并百度影音的离线数据( with python 2.3)
2015/08/04 Python
python logging 日志轮转文件不删除问题的解决方法
2016/08/02 Python
python遍历文件目录、批量处理同类文件
2019/08/31 Python
opencv 查找连通区域 最大面积实例
2020/06/04 Python
python 对象真假值的实例(哪些视为False)
2020/12/11 Python
python 写一个水果忍者游戏
2021/01/13 Python
澳大利亚家具和家居用品在线商店:Interiors Online
2018/03/05 全球购物
拥有超过850家商店的美国在线派对商店:Party City
2018/10/21 全球购物
Herschel美国官网:背包、手提袋及配件
2020/03/10 全球购物
三年级作文之趣事作文
2019/11/04 职场文书
Pyhton模块和包相关知识总结
2021/05/12 Python
mybatis 解决从列名到属性名的自动映射失败问题
2021/06/30 Java/Android
详解Flutter网络请求Dio库的使用及封装
2022/04/14 Java/Android