基于vue怎么写一个简单购物车,代码是什么
Admin 2022-06-28 群英技术资讯 531 次浏览
本文实例为大家分享了Vue实现简单购物车的具体代码,供大家参考,具体内容如下
HTML首页
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="/css/index.css" > </head> <body> <div id="app"> <div v-if="books.length != 0"> <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>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.date}}</td> <td>{{item.price | showPrice}}</td> <td> <button @click="decrement(index)" :disabled="item.count <= 1">-</button> {{item.count}} <button @click="increment(index)">+</button> </td> <td><button @click="removeHandle(index)">移除</button></td> </tr> </tbody> </table> <h2>总价格为:{{totalPrice | showPrice}}</h2> </div> <h2 v-else>购物车为空</h2> </div> <script src="/js/vue.js"></script> <script src="/js/index.js"></script> </body> </html>
css代码
* { margin: 0; padding: 0; } table { margin: 100px 0 0 100px; border: 1px solid #e9e9e9; border-collapse: collapse; border-spacing: 0; } th, td { padding: 8px 16px; border: 1px solid #e9e9e9; text-align: left; } th { background-color: #f7f7f7; color: black; font-weight: 6000 ; } h2 { width: 500px; margin-left: 100px; } button { padding: 5px; }
js代码(Vue)
const app = new Vue({ el:"#app", data:{ books:[ { id:1, name:'《算法导论》', date:'2019-2', price:85.00, count:1 }, { id:2, name:'《计算机基础》', date:'2019-2', price:95.00, count:1 }, { id:3, name:'《c++高级语言》', date:'2019-2', price:89.00, count:1 }, { id:4, name:'《编译原理》', date:'2019-2', price:77.00, count:1 }, ] }, methods:{ decrement(index){ this.books[index].count-- }, increment(index){ this.books[index].count++ }, removeHandle(index){ this.books.splice(index,1) } }, computed:{ totalPrice(){ let finalPrice = 0 for(let i = 0; i < this.books.length; i++){ finalPrice += this.books[i].price * this.books[i].count } return finalPrice } }, filters:{ showPrice(price){ return '¥' + price.toFixed(2) } } })
运行结果
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章给大家分享的是JavaScript中字符转浮点型的方法,文中给大家介绍了两种方法,一种是使用parseFloat()方法,另一种是利用“+”运算符,对新手学习JavaScript有一定的参考价值,文中的示例介绍得很详细,有需要的朋友可以了解看看,接下来就跟随小编一起学习一下吧。
这篇文章主要为大家详细介绍了微信小程序实现底部弹窗,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
学过 vue2 的小伙伴们肯定学习过侦听器,主要是用来监听页面数据或者是路由的变化,来执行相应的操作,在 vue3里面呢,也有侦听器的用法,功能基本一样,本文就来为大家详细讲讲
这篇文章主要介绍了详解CocosCreator华容道数字拼盘,对华容道感兴趣的同学,看完之后,可以回去亲手试一下
接金币游戏的游戏,相信不少朋友都有玩过,或者玩过类似的,也就是屏幕上方掉落进步,移动下方接金币区域来接住金币。那么如果我们用JavaScript如何实现呢?下面就给大家分享如何用JS做一个接金币游戏。
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008