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实现购物车实例代码两则

如上,所有书类数据存在数组里,遍历显示在表格中,点击+和-可以实现数量和总价格的响应式变化,其中,减号到1时便添加了disabled类型,无法点击。 价格显示时通过过滤器显示的,加上Z¥符号和两位小数。项目结构为三个文件。

index.html

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
 <link rel="stylesheet" href="style.css" rel="external nofollow" >
</head>
<body>
<div id="app">
 <div v-if="books.length">
 <table>
 <thead>
 <tr>
 <th></th>
 <th>书籍日期</th>
 <th>出版日期</th>
 <th>价格</th>
 <th>购买数量</th>
 <th>操作 </th>
 </tr>
 </thead>

 <tbody>
 <tr v-for="(item,index) in books">
 <!--<td v-for="value in item">{{value}}</td>-->
 <td>{{item.id}}</td>
 <td>{{item.name}}</td>
 <td>{{item.date}}</td>
 <!--<td>{{getFinalPrice(item.price)}}</td> 下面是过滤器语法-->
 <td>{{item.price | showPrice}}</td>
 <td>
 <button @click="increment(index)" v-bind:disabled="item.count<=1">-</button>
 {{item.count}}
 <button @click="decrement(index)">+</button>
 </td>
 <td>
 <button @click="removeHandler(index)">移除</button>
 </td>
 </tr>
 </tbody>
 </table>
 <h2>总价格: {{totalprice | showPrice}}</h2>
 </div>
 <h2 v-else> 购物车为空</h2>
</div>
<script src="../js/vue.js"></script>
<script src="main.js"></script>
<script></script>
</body>
</html>

main.js

const app = new Vue({
 el:"#app",
 data: {
 books: [
 {
 id: 1,
 name: '算法导论',
 date: '2019-01-10',
 price: 85.00,
 count: 1
 },
 {
 id: 2,
 name: '计算机导论',
 date: '2019-02-14',
 price: 90.00,
 count: 2
 },
 {
 id: 3,
 name: '科学导论',
 date: '2019-09-10',
 price: 85.21,
 count: 1
 },
 {
 id: 4,
 name: '网络导论',
 date: '2019-08-21',
 price: 19.02,
 count: 1
 },
 ]
 },
 methods:{
 getFinalPrice(price) {
 return '$' + price.toFixed(2)
 },
 increment(index){
 /*if(this.books[index].count <= 1) return*/
 this.books[index].count--
 },
 decrement(index){
 this.books[index].count++
 },
 removeHandler(index){
 this.books.splice(index,1)
 }
 },
 filters:{
 showPrice(price){
 return '$' + price.toFixed(2)
 }
 },
 computed:{
 totalprice(){
 let tprice = 0
 for(let i = 0; i< this.books.length; i++)
 {
 tprice += this.books[i].price * this.books[i].count
 }
 return tprice
 }
 }
})

style.css

table{
 border: 1px solid #e9e9e9;
 border-collapse: collapse;
 bordre-spacing: 0;
}

th, td {
 padding: 8px 16px;
 border: 1px solid #e9e9e9;
 text-align: left;
}

th{
 backgroud-color: #f7f7f7;
 color: #5c6b77;
 font-weight: 600;
}

到此这篇关于Vue实现购物车实例代码的文章就介绍到这了,更多相关Vue 购物车内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
有趣的JavaScript数组长度问题代码说明
Jan 20 Javascript
nodeType属性返回被选节点的节点类型介绍
Nov 22 Javascript
超赞的jQuery图片滑块动画特效代码汇总
Jan 25 Javascript
javascript的正则匹配方法学习
Feb 24 Javascript
Bootstrap Chart组件使用教程
Apr 28 Javascript
jQuery解析与处理服务器端返回xml格式数据的方法详解
Jul 04 Javascript
原生JS实现的放大镜效果实例代码
Oct 15 Javascript
JS正则匹配URL网址的方法(可匹配www,http开头的一切网址)
Jan 06 Javascript
Angular 容器部署的方法
Apr 17 Javascript
Js中将Long转换成日期格式的实现方法
Jun 05 Javascript
微信小程序Getuserinfo解决方案图解
Aug 24 Javascript
VSCode使用之Vue工程配置eslint
Apr 30 Javascript
vue实现购物车案例
May 30 #Javascript
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
You might like
初识laravel5
2015/03/02 PHP
求帮忙修改个php curl模拟post请求内容后并下载文件的解决思路
2015/09/20 PHP
php生成酷炫的四个字符验证码
2016/04/22 PHP
php5.3后静态绑定用法详解
2016/11/11 PHP
WAF的正确bypass
2017/01/05 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
2020/02/21 PHP
JS定时刷新页面及跳转页面的方法
2013/07/04 Javascript
javascript使用call调用微信API
2014/12/15 Javascript
调试JavaScript中正则表达式中遇到的问题
2015/01/27 Javascript
jquery实现翻动fadeIn显示的方法
2015/03/05 Javascript
jQuery的each循环用法简单示例
2016/06/12 Javascript
基于jQuery实现顶部导航栏功能
2016/12/27 Javascript
Vue.js如何优雅的进行form validation
2017/04/07 Javascript
Angualrjs 表单验证的两种方式(失去焦点验证和点击提交验证)
2017/05/09 Javascript
jQuery实现的回车触发按钮事件功能示例
2018/03/25 jQuery
详解React Native 屏幕适配(炒鸡简单的方法)
2018/06/11 Javascript
使用JS监听键盘按下事件(keydown event)
2019/11/07 Javascript
Node.js API详解之 string_decoder用法实例分析
2020/04/29 Javascript
react-intl实现React国际化多语言的方法
2020/09/27 Javascript
通过实例解析js可枚举属性与不可枚举属性
2020/12/02 Javascript
[02:15]2015国际邀请赛选手档案IG.Ferrari 430
2015/07/30 DOTA
[03:44]2015国际邀请赛选手档案—Cloud9.NoTail
2015/07/28 DOTA
使用NumPy和pandas对CSV文件进行写操作的实例
2018/06/14 Python
Python线上环境使用日志的及配置文件
2019/07/28 Python
python3.6 tkinter实现屏保小程序
2019/07/30 Python
python3.8下载及安装步骤详解
2020/01/15 Python
解决pycharm下pyuic工具使用的问题
2020/04/08 Python
详解pandas获取Dataframe元素值的几种方法
2020/06/14 Python
毕业自荐信
2013/12/16 职场文书
心理健康活动总结
2014/04/30 职场文书
习近平在党的群众路线教育实践活动总结大会上的讲话全文
2014/10/25 职场文书
2016小学新学期寄语
2015/12/04 职场文书
创业计划书之溜冰场
2019/10/25 职场文书
解决Pytorch中关于model.eval的问题
2021/05/22 Python
Python爬虫之自动爬取某车之家各车销售数据
2021/06/02 Python
Nginx隐藏式跳转(浏览器URL跳转后保持不变)
2022/04/07 Servers