Vue實(shí)現(xiàn)簡單購物車功能
本文實(shí)例為大家分享了Vue實(shí)現(xiàn)簡單購物車功能的具體代碼,供大家參考,具體內(nèi)容如下
話不多少,上效果圖
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet"> </head> <body> <div id="app"> <div v-if="books.length"> <table class="table table-dark"> <thead> <tr> <th scope="col">ID</th> <th scope="col">書籍名稱</th> <th scope="col">出版日期</th> <th scope="col">書籍價格</th> <th scope="col">購買數(shù)量</th> <th scope="col">操作</th> </tr> </thead> <tbody> <tr v-for="(item,index) in books"> <th scope="row">{{item.id}}</th> <td>{{item.name}}</td> <td>{{item.date}}</td> <td>{{item.price | dealPrice}}</td> <td> <button class="btn btn-primary" @click="decrement(index)" :disabled="item.count <= 0">-</button> {{item.count}} <button class="btn btn-primary" @click="increment(index)">+</button> </td> <td> <button class="btn btn-danger" @click="removeBook(index)">移除</button> </td> </tr> </tbody> </table> <h2>總價為 {{totalPrice | dealPrice}}</h2> </div> <h2 v-else>購物車為空</h2> </div> <script src="vue.js"></script> <script> const app = new Vue({ el:'#app', data:{ books:[ { id:1, name:'PHP手冊', date:'2020年5月17號', price:33, count:1 }, { id:2, name:'Python手冊', date:'2020年5月17號', price:33, count:1 }, { id:3, name:'Linux手冊', date:'2020年5月17號', price:33, count:1 }, ], }, computed:{ totalPrice(){ let price = 0; for (let i = 0;i<this.books.length;i++) { price += this.books[i].price * this.books[i].count } return price; } }, methods:{ increment(index){ this.books[index].count ++ }, decrement(index) { this.books[index].count -- }, removeBook(index) { this.books.splice(index,1) } }, filters: { dealPrice(price){ return '$' + price.toFixed(2) } } }) </script> </body> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue父組件給子組件的組件傳值provide inject的方法
在本篇文章里小編給大家整理的是一篇關(guān)于vue父組件給子組件的組件傳值provide inject的方法,需要的朋友們學(xué)習(xí)下。2019-10-10Vue.js最佳實(shí)踐(五招助你成為vuejs大師)
這篇文章主要介紹了Vue.js最佳實(shí)踐,本文主要面向?qū)ο笫怯幸欢╲ue.js 編輯經(jīng)驗的開發(fā)者,需要的朋友可以參考下2018-05-05vue?elementui動態(tài)添加el-input實(shí)例代碼
最近遇到一個新的需求,需要動態(tài)添加el-input,這篇文章主要給大家介紹了關(guān)于vue?elementui動態(tài)添加el-input的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能
這篇文章主要為大家詳細(xì)介紹了vue使用vuedraggable實(shí)現(xiàn)嵌套多層拖拽排序功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04Vue CLI3基礎(chǔ)學(xué)習(xí)之pages構(gòu)建多頁應(yīng)用
這篇文章主要給大家介紹了關(guān)于Vue CLI3基礎(chǔ)學(xué)習(xí)之pages構(gòu)建多頁應(yīng)用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue CLI3具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06VUE + OPENLAYERS實(shí)現(xiàn)實(shí)時定位功能
本系列文章介紹一個簡單的實(shí)時定位示例,基于VUE + OPENLAYERS實(shí)現(xiàn)實(shí)時定位功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-09-09vue使用elementUI分頁如何實(shí)現(xiàn)切換頁面時返回頁面頂部
這篇文章主要介紹了vue使用elementUI分頁如何實(shí)現(xiàn)切換頁面時返回頁面頂部,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08