十二、Vue 实现 购物车demo
1. html代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | <!DOCTYPE html> <html lang= "en" > <head> <meta charset= "UTF-8" > <title></title> <link rel= "stylesheet" href= "style.css" > </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> {{ item.id }} </td> <td> {{ item.name }} </td> <td> {{ item.date }} </td> <!-- <td> {{ getFinalPrice(item.price) }} </td> --> <!-- <td> {{ item.price | 过滤器 }} </td> --> <td> {{ item.price | showPrice }} <td> <button @click= "increment(index)" >+</button> {{ item.sumNumber }} <button @click= "decrement(index)" v-bind:disabled= "item.sumNumber<=1" >-</button> </td> <td> <button @click= "removeHandle(index)" >移除</button> </td> </tr> </tbody> </table> <h2>总价格:{{ totalSumPrice | showPrice }}</h2> </div> <div v- else > <h2>购物车为空</h2> </div> </div> <script src= "../jquery/vue.JS" ></script> <script src= "main.js" ></script> </body> </html> |
2. css代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | table{ border: 1px solid #e9e9e9; border-collapse: collapse; border-spacing: 0; } th, td{ padding: 8px 16px; border: 1px solid #e9e9e9; text-align: center; } th{ background-color: #f7f7f7; color: #5c6b77; font-weight: 600; } |
3. js代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | const app = new Vue({ el: "#app" , data: { books: [ {id: 1, name: "《算法导论》" , date: "2006-9" , price: 85.00, sumNumber:1}, {id: 2, name: "《UNIX编程艺术》" , date: "2006-2" , price: 59.00, sumNumber:1}, {id: 3, name: "《变成注记》" , date: "2008-10" , price: 39.00, sumNumber:1}, {id: 4, name: "《代码大全》" , date: "2006-3" , price: 128.00, sumNumber:1}, ], }, methods: { // getFinalPrice(price){ // return "¥" + price.toFixed(2); // }, increment(index){ this .books[index].sumNumber++; }, decrement(index){ this .books[index].sumNumber--; }, removeHandle(index){ this .books.splice(index,1); } }, computed: { totalSumPrice(){ let totalprice = 0; // //1. 普通的for循环 // for(let i=0; i<this.books.length; i++){ // totalprice += this.books[i].sumNumber * this.books[i].price; // } // //2. for(let i in this.books){} // for(let i in this.books){ // totalprice += this.books[i].sumNumber * this.books[i].price; // } // //3. for(let i of this.books){} // for(let book of this.books){ // totalprice += book.sumNumber * book.price; // } // return totalprice; //4. reduce return this .books.reduce(function(preValue,book){ return preValue + book.sumNumber * book.price; },0) } }, filters: { //过滤器、一般为函数 showPrice(price){ return "¥" + price.toFixed(2); } } }); |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通