Vue實現(xiàn)簡單購物車小案例
本文實例為大家分享了Vue實現(xiàn)簡單購物車的具體代碼,供大家參考,具體內(nèi)容如下
HTML首頁
<!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> <link rel="stylesheet" href="/css/index.css" > </head> <body> <div id="app"> <div v-if="books.length != 0"> <table> <thead> <tr> <th></th> <th>書籍名稱</th> <th>出版如期</th> <th>價格</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 | showPrice}}</td> <td> <button @click="decrement(index)" :disabled="item.count <= 1">-</button> {{item.count}} <button @click="increment(index)">+</button> </td> <td><button @click="removeHandle(index)">移除</button></td> </tr> </tbody> </table> <h2>總價格為:{{totalPrice | showPrice}}</h2> </div> <h2 v-else>購物車為空</h2> </div> <script src="/js/vue.js"></script> <script src="/js/index.js"></script> </body> </html>
css代碼
* { margin: 0; padding: 0; } table { margin: 100px 0 0 100px; border: 1px solid #e9e9e9; border-collapse: collapse; border-spacing: 0; } th, td { padding: 8px 16px; border: 1px solid #e9e9e9; text-align: left; } th { background-color: #f7f7f7; color: black; font-weight: 6000 ; } h2 { width: 500px; margin-left: 100px; } button { padding: 5px; }
js代碼(Vue)
const app = new Vue({ el:"#app", data:{ books:[ { id:1, name:'《算法導(dǎo)論》', date:'2019-2', price:85.00, count:1 }, { id:2, name:'《計算機(jī)基礎(chǔ)》', date:'2019-2', price:95.00, count:1 }, { id:3, name:'《c++高級語言》', date:'2019-2', price:89.00, count:1 }, { id:4, name:'《編譯原理》', date:'2019-2', price:77.00, count:1 }, ] }, methods:{ decrement(index){ this.books[index].count-- }, increment(index){ this.books[index].count++ }, removeHandle(index){ this.books.splice(index,1) } }, computed:{ totalPrice(){ let finalPrice = 0 for(let i = 0; i < this.books.length; i++){ finalPrice += this.books[i].price * this.books[i].count } return finalPrice } }, filters:{ showPrice(price){ return '¥' + price.toFixed(2) } } })
運行結(jié)果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js 實現(xiàn)v-model與{{}}指令方法
這篇文章主要介紹了vue.js 實現(xiàn)v-model與{{}}指令方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10Vue使用Print.js打印div方式(選中區(qū)域的html)
這篇文章主要介紹了Vue使用Print.js打印div方式(選中區(qū)域的html),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03在vue中利用全局路由鉤子給url統(tǒng)一添加公共參數(shù)的例子
今天小編就為大家分享一篇在vue中利用全局路由鉤子給url統(tǒng)一添加公共參數(shù)的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11VUE3使用Element-Plus時如何修改ElMessage中的文字大小
在使用Element-plus的Elmessage時使用默認(rèn)的size無法滿足我們的需求時,我們可以自定義字體的大小,但是直接重寫樣式后,并沒有起作用,甚至使用::v-deep深度選擇器也沒有效果,本文介紹VUE3使用Element-Plus時如何修改ElMessage中的文字大小,感興趣的朋友一起看看吧2023-09-09Vue2.5 結(jié)合 Element UI 之 Table 和 Pagination 組件實現(xiàn)分頁功能
這篇文章主要介紹了Vue2.5 結(jié)合 Element UI 之 Table 和 Pagination 組件實現(xiàn)分頁功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2018-01-01詳解vue2.0 資源文件assets和static的區(qū)別
這篇文章主要介紹了詳解vue2.0 資源文件assets和static的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11