Vue實(shí)現(xiàn)小購物車功能
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)小購物車功能的具體代碼,供大家參考,具體內(nèi)容如下
本人還在初級(jí)學(xué)習(xí)階段,有很多不足之處,希望能指出錯(cuò)誤,一起進(jìn)步
HTML代碼塊
<body> <div id="app"> <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="(itme,index) in books"> <td>{{itme.id}}</td> <td>{{itme.name}}</td> <td>{{itme.date}}</td> <!-- showPrice過濾器 --> <td>{{itme.price | showPrice}}</td> <td> <!-- 動(dòng)態(tài)綁定disabled,當(dāng)數(shù)量小于1時(shí) 禁止點(diǎn)擊按鈕--> <button @click="decrement(index)" :disabled="itme.count <= 1">-</button> {{itme.count}} <button @click="increment(index)">+</button> </td> <td><button @click="Handle(index)">移除</button></td> </tr> </tbody> </table> <h2>總價(jià)格:{{totalPrice | showPrice}}</h2> </div> <h2 v-else>購物車為空</h2> </div> </body>
css代碼塊
table{ border: 1px solid #e9e9e9; border-collapse: collapse; border-spacing: 0; } th,td{ padding: 8px 16px; border: 1px solid #e9e9e9; text-align: left; } th{ background: #f7f7f7; color: #5c6b77; font-weight: 600; }
Vue.js代碼塊
const app = new Vue({ el:'#app', data:{ books:[ { id:1, name:'《算法議論》', date:'2001-1', price:85.00, count:1 }, { id:2, name:'《編程珠璣》', date:'2002-1', price:65.00, count:1 }, { id:3, name:'《Unix編程藝術(shù)》', date:'2000-1', price:59.00, count:1 }, { id:4, name:'《代碼大全》', date:'2005-1', price:135.00, count:1 }, ] }, /** * 使用普通方法 */ methods:{ //獲取小數(shù)點(diǎn)的方法 // getFinalPrice(price){ // return '¥' + price.toFixed(2); // } //點(diǎn)擊事件 increment(index){ this.books[index].count++; }, decrement(index){ this.books[index].count--; }, Handle(index){ this.books.splice(index,1); } }, computed:{ totalPrice(){ let totalPrice = 0; for(let i = 0; i < this.books.length; i++){ totalPrice += this.books[i].price * this.books[i].count; } return totalPrice; } }, /** * 使用過濾器獲取小數(shù)點(diǎn) */ filters:{ showPrice(price){ return '¥' + price.toFixed(2); } } })
效果如圖:
還有很多不足之處,希望大家能指出。
關(guān)于vue.js的學(xué)習(xí)教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程、Vue.js前端組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue.js 實(shí)現(xiàn)地址管理頁面思路詳解(地址添加、編輯、刪除和設(shè)置默認(rèn)地址)
這篇文章主要介紹了Vue.js 實(shí)現(xiàn)地址管理頁面的思路(地址添加、編輯、刪除和設(shè)置默認(rèn)地址),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12Vue實(shí)例中el和data的兩種寫法小結(jié)
這篇文章主要介紹了Vue實(shí)例中el和data的兩種寫法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11Vue封裝一個(gè)Tabbar組件?帶組件路由跳轉(zhuǎn)方式
這篇文章主要介紹了Vue封裝一個(gè)Tabbar組件?帶組件路由跳轉(zhuǎn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04element ui里dialog關(guān)閉后清除驗(yàn)證條件方法
下面小編就為大家分享一篇element ui里dialog關(guān)閉后清除驗(yàn)證條件方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-02-02vue echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)展示
這篇文章主要為大家詳細(xì)介紹了vue echarts實(shí)現(xiàn)柱狀圖動(dòng)態(tài)展示,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09教你如何開發(fā)Vite3插件構(gòu)建Electron開發(fā)環(huán)境
這篇文章主要介紹了如何開發(fā)Vite3插件構(gòu)建Electron開發(fā)環(huán)境,文中給大家提到了如何讓 Vite 加載 Electron 的內(nèi)置模塊和 Node.js 的內(nèi)置模塊,需要的朋友可以參考下2022-11-11