您好,登錄后才能下訂單哦!
這篇文章主要講解了“怎么用vue實(shí)現(xiàn)可改變購物數(shù)量的購物車”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“怎么用vue實(shí)現(xiàn)可改變購物數(shù)量的購物車”吧!
本文實(shí)例為大家分享了vue實(shí)現(xiàn)改變購物數(shù)量的購物車,供大家參考,具體內(nèi)容如下
1.computed 計(jì)算屬性
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>價(jià)格</th> <th>購買數(shù)量</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>總價(jià):{{getallprice | toprice}}</h3> </div> <h3 v-else>您沒有購物信息</h3> </div> <script> const vm = new Vue({ el: "#box", data: { books: [{ id: 1, name: "《vue.js實(shí)戰(zhàn)》", date: "2010.2.4", price: 82.00, aunt: 1 }, { id: 2, name: "《javascript實(shí)戰(zhàn)》", date: "2010.2.4", price: 108.00, aunt: 1 }, { id: 3, name: "《html+css實(shí)戰(zhàn)》", date: "2010.2.4", price: 42.50, aunt: 1 }, { id: 4, name: "《axios實(shí)戰(zhàn)》", date: "2010.2.4", price: 82.00, aunt: 1 }, { id: 5, name: "《jquery實(shí)戰(zhàn)》", 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實(shí)現(xiàn)可改變購物數(shù)量的購物車”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)怎么用vue實(shí)現(xiàn)可改變購物數(shù)量的購物車這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。