vue實(shí)現(xiàn)可改變購(gòu)物數(shù)量的購(gòu)物車
本文實(shí)例為大家分享了vue實(shí)現(xiàn)改變購(gòu)物數(shù)量的購(gòu)物車,供大家參考,具體內(nèi)容如下
效果圖:
知識(shí)點(diǎn):
1.computed 計(jì)算屬性
2.filters 過濾器
實(shí)現(xiàn)代碼:
<!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>購(gòu)買數(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> <h2>總價(jià):{{getallprice | toprice}}</h2> </div> <h2 v-else>您沒有購(gòu)物信息</h2> </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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vuejs手把手教你寫一個(gè)完整的購(gòu)物車實(shí)例代碼
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車功能
- Vue實(shí)現(xiàn)購(gòu)物車功能
- vue實(shí)現(xiàn)購(gòu)物車小案例
- vue實(shí)現(xiàn)商城購(gòu)物車功能
- vue 實(shí)現(xiàn)購(gòu)物車總價(jià)計(jì)算
- vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車功能示例
- vue+vant-UI框架實(shí)現(xiàn)購(gòu)物車的復(fù)選框全選和反選功能
- Vue.js實(shí)現(xiàn)的購(gòu)物車功能詳解
- Vuejs實(shí)現(xiàn)購(gòu)物車功能
相關(guān)文章
關(guān)于echarts?清空上一次加載的數(shù)據(jù)問題
這篇文章主要介紹了關(guān)于echarts?清空上一次加載的數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10element?ui富文本編輯器的使用效果與步驟(quill-editor)
富文本編輯器在任何項(xiàng)目中都會(huì)用到,在Element中我們推薦vue-quill-editor組件,下面這篇文章主要給大家介紹了關(guān)于element?ui富文本編輯器的使用效果與步驟(quill-editor)的相關(guān)資料,需要的朋友可以參考下2022-10-10vue阻止頁(yè)面回退的實(shí)現(xiàn)方法(瀏覽器適用)
這篇文章主要介紹了vue阻止頁(yè)面回退的實(shí)現(xiàn)方法(瀏覽器適用),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Vue3+antDesignVue實(shí)現(xiàn)表單校驗(yàn)的方法
這篇文章主要為大家詳細(xì)介紹了基于Vue3和antDesignVue實(shí)現(xiàn)表單校驗(yàn)的方法,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,需要的小伙伴可以了解下2024-01-01vue通過子組件修改父組件prop的多種實(shí)現(xiàn)方式
這篇文章主要介紹了vue通過子組件修改父組件prop的幾種實(shí)現(xiàn)方式,比較常用的方式是通過Prop單向傳遞的規(guī)則,需要的朋友可以參考下2021-09-09Vue使用axios進(jìn)行g(shù)et請(qǐng)求拼接參數(shù)的2種方式詳解
axios中post請(qǐng)求都是要求攜帶參數(shù)進(jìn)行請(qǐng)求,這篇文章主要給大家介紹了關(guān)于Vue使用axios進(jìn)行g(shù)et請(qǐng)求拼接參數(shù)的2種方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01