vue實(shí)現(xiàn)書籍購(gòu)物車功能
本文實(shí)例為大家分享了vue實(shí)現(xiàn)書籍購(gòu)物車功能的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
點(diǎn)擊增加、減少購(gòu)買數(shù)量和移除總價(jià)格會(huì)變化
代碼
<!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>書籍購(gòu)物車</title> <style> 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-color: #f7f7f7; color: #5c6b77; font-weight: 600; } </style> </head> <body> <div id="app" v-cloak> <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>{{index+1}}</td> <td>{{item.name}}</td> <td>{{item.date}}</td> <td>{{item.price | showPrice}}</td> <td> <!-- disabled 為true時(shí)按鈕禁用 --> <button @click="reduce(index)" v-bind:disabled="item.count <= 1">-</button> {{item.count}} <button @click="increase(index)">+</button> </td> <td><button @click="remove(index)">移除</button></td> </tr> </tbody> </table> <h2>總價(jià)格:{{totalPrice | showPrice}}</h2> </div> <h2 v-else>購(gòu)物車為空</h2> </div> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> const app = new Vue({ el: "#app", data:{ books:[ { name: '《算法導(dǎo)論》', date: '2021-8-1', price: 85.00, count: 1 }, { name: '《UNIX編程藝術(shù)》', date: '2021-8-2', price: 69.00, count: 1 }, { name: '《編程珠璣》', date: '2021-8-3', price: 35.00, count: 1 }, { name: '《DOM編程藝術(shù)》', date: '2021-8-4', price: 75.00, count: 1 }, { name: '《nodejs深入淺出》', date: '2021-8-5', price: 105.00, count: 1 }, ], }, methods:{ reduce(index){ this.books[index].count--; }, increase(index){ this.books[index].count++; }, remove(index){ this.books.splice(index,1); }, }, computed:{ // 寫在計(jì)算屬性里的方法可以直接當(dāng)屬性使用 totalPrice(){ //let totalPrice = 0; // 1. 普通的for循環(huán) // for (let i = 0; i < this.books.length; i++) { // totalPrice += this.books[i].count * this.books[i].price; // } // 2. 步驟稍簡(jiǎn)單的普通for循環(huán) // for (let i in this.books) { // totalPrice += this.books[i].count * this.books[i].price; // } // 3. for(let item of this.books) //for(let item of this.books){ //totalPrice += item.count * item.price; //} //return totalPrice; // 4. 高階函數(shù)寫法 reduce // 直接返回結(jié)果 不需要定義變量,也不需要遍歷 return this.books.reduce(function(pre, book){ return pre + book.price * book.count; },0); }, // 過(guò)濾器 filters:{ showPrice(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實(shí)現(xiàn)購(gòu)物車拋物線小球動(dòng)畫效果的方法詳解
- Vue實(shí)現(xiàn)購(gòu)物車詳情頁(yè)面的方法
相關(guān)文章
vue3??mark.js?實(shí)現(xiàn)文字標(biāo)注功能(案例代碼)
這篇文章主要介紹了vue3??mark.js?實(shí)現(xiàn)文字標(biāo)注功能,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-09-09詳解如何在vue項(xiàng)目中使用eslint+prettier格式化代碼
在開(kāi)發(fā)中我們需要一種能夠統(tǒng)一團(tuán)隊(duì)代碼風(fēng)格的工具,作為強(qiáng)制性的規(guī)范,統(tǒng)一整個(gè)項(xiàng)目的代碼風(fēng)格,這篇文章主要介紹了詳解如何在vue項(xiàng)目中使用eslint+prettier格式化代碼,需要的朋友可以參考下2018-11-11解決VUEX兼容IE上的報(bào)錯(cuò)問(wèn)題
下面小編就為大家分享一篇解決VUEX兼容IE上的報(bào)錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03vue3?使用setup語(yǔ)法糖實(shí)現(xiàn)分類管理功能
這篇文章主要介紹了vue3?使用setup語(yǔ)法糖實(shí)現(xiàn)分類管理,本次模塊使用 vue3+element-plus 實(shí)現(xiàn)一個(gè)新聞?wù)镜暮笈_(tái)分類管理模塊,其中新增、編輯采用對(duì)話框方式公用一個(gè)表單,需要的朋友可以參考下2022-08-08淺析Proxy如何實(shí)現(xiàn)Vue響應(yīng)式
這篇文章主要是來(lái)和大家探討一下,Vue的響應(yīng)式系統(tǒng)僅僅是一個(gè)Proxy嗎,本文將圍繞此問(wèn)題探索一下Proxy是如何實(shí)現(xiàn)Vue響應(yīng)式的,感興趣的小伙伴可以了解一下2023-08-08vue2實(shí)現(xiàn)無(wú)感刷新token的方式詳解
在Web應(yīng)用中,用戶需要通過(guò)認(rèn)證和授權(quán)才能訪問(wèn)受保護(hù)的資源,為了實(shí)現(xiàn)認(rèn)證和授權(quán)功能,通常需要使用Token來(lái)標(biāo)識(shí)用戶身份并驗(yàn)證其權(quán)限,本文給大家介紹了vue2實(shí)現(xiàn)無(wú)感刷新token的方式,需要的朋友可以參考下2024-02-02Vue3+TS實(shí)現(xiàn)語(yǔ)音播放組件的示例代碼
這篇文章主要介紹了如何利用Vue+TS實(shí)現(xiàn)一個(gè)簡(jiǎn)易的語(yǔ)音播放組件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Vue有一定的幫助,需要的可以參考一下2022-03-03vsCode中vue文件無(wú)法提示html標(biāo)簽的操作方法
在vsCode中書寫Vue頁(yè)面時(shí)無(wú)法提示,那真是很郁悶的事情,下面這篇文章主要給大家介紹了關(guān)于vsCode中vue文件無(wú)法提示html標(biāo)簽的操作方法,需要的朋友可以參考下2023-03-03