您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“怎么用vue实现可改变购物数量的购物车”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用vue实现可改变购物数量的购物车”吧!
本文实例为大家分享了vue实现改变购物数量的购物车,供大家参考,具体内容如下
1.computed 计算属性
2.filters 过滤器
<!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> </head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <style> table { border-collapse: collapse; border-spacing: 0; border: 1px solid #ccc; } td, th { padding: 8px 16px; border: 1px solid #ccc; text-align: left; } th { background-color: #f7f7f7; color: #5c6b77; } </style> <body> <div id="box"> <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>{{item.id}}</td> <td>{{item.name}}</td> <td>{{item.date}}</td> <td>{{item.price | toprice}}</td> <td> <button @click='down(index)' :disabled="item.aunt<=1">-</button> {{item.aunt}} <button @click='add(index)'>+</button> </td> <td> <button @click="remove(index)">移除</button> </td> </tr> </tbody> </table> <h3>总价:{{getallprice | toprice}}</h3> </div> <h3 v-else>您没有购物信息</h3> </div> <script> const vm = new Vue({ el: "#box", data: { books: [{ id: 1, name: "《vue.js实战》", date: "2010.2.4", price: 82.00, aunt: 1 }, { id: 2, name: "《javascript实战》", date: "2010.2.4", price: 108.00, aunt: 1 }, { id: 3, name: "《html+css实战》", date: "2010.2.4", price: 42.50, aunt: 1 }, { id: 4, name: "《axios实战》", date: "2010.2.4", price: 82.00, aunt: 1 }, { id: 5, name: "《jquery实战》", date: "2010.2.4", price: 65.20, aunt: 1 }, ] }, methods: { add(index) { this.books[index].aunt++; }, down(index) { this.books[index].aunt--; }, remove(index) { this.books.splice(index, 1) }, }, computed: { getallprice() { let all = 0; for (let i = 0; i < this.books.length; i++) { all += this.books[i].price * this.books[i].aunt } return all } }, filters: { toprice(price) { return '¥' + price.toFixed(2) }, } }) </script> </body> </html>
感谢各位的阅读,以上就是“怎么用vue实现可改变购物数量的购物车”的内容了,经过本文的学习后,相信大家对怎么用vue实现可改变购物数量的购物车这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。