av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Vue實現簡易購物車案例

瀏覽:12日期:2022-09-29 09:47:17

本文實例為大家分享了Vue實現簡易購物車的具體代碼,供大家參考,具體內容如下

先來看一下完成后的效果吧。

Vue實現簡易購物車案例

CSS 部分

這里沒什么好說的,就是v-cloak 這一個知識點

table{ border: 1px solid #e9e9e9; border-collapse: collapse; border-spacing: 0;}th,td{ padding: 8px 16px; border: 1px solid #e9e9e9; text-align: center;}th{ background-color: #f7f7f7; color: #5c6b77; font-weight: 600;}[v-cloak]{ display: none;}HTML部分

這里說明一些用到的一些Vue的知識點:

v-if v-for v-cloak v-on > @ v-bind > : 方法 methods 計算屬性 computed 過濾器 filters

<!DOCTYPE html><html lang='en'><head> <meta charset='UTF-8'> <meta name='viewport' content='width=device-width, initial-scale=1.0'> <title>購物車</title> <link rel='stylesheet' href='http://www.4tl426be.cn/bcjs/style.css' ></head><body> <div v-cloak> <div v-if='books.length'> <table><thead> <tr> <th></th> <th>書籍名稱</th> <th>出版日期</th> <th>價格</th> <th>購買數量</th> <th>刪除</th> </tr></thead><tbody> <tr v-for='(item,index) in books'> <th>{{item.id}}</th> <th>{{item.name}}</th> <th>{{item.date}}</th> <!--方案一 保留小數點和貨幣符號--> <!-- <th>{{'¥'+item.price.toFixed(2)}}</th> --> <!--方案二--> <!-- <th>{{getFinalPrice(item.price)}}</th> --> <!--方案三--> <th>{{item.price | showPrice}}</th> <th> <button @click='decrement(index)' :disabled='item.count<=0'>-</button> {{item.count}} <button @click='increment(index)'>+</button> </th> <th><button @click='removeHandle(index)'>移除</button></th> </tr></tbody> </table> <h2>總價格:{{totalPrice | showPrice}}</h2> </div> <h2 v-else> 購物車為空 </h2> </div></body><script src='http://www.4tl426be.cn/js/vue.js'></script><script src='http://www.4tl426be.cn/bcjs/main.js'></script></html>JS部分

const app = new Vue({ el:'#app', data:{ books:[ {id:1,name:'《算法導論》',date:’2006-9’,price:85.00,count:1 }, {id:2,name:'《UNIX編程藝術》',date:’2006-2’,price:50.00,count:1 }, {id:3,name:'《編程藝術》',date:’2008-10’,price:39.00,count:1 }, {id:4,name:'《代碼大全》',date:’2006-3’,price:128.00,count:1 }, ] }, methods: { //這里我們放棄使用方法的形式來求總價格,轉而使用計算屬性,因為它的效率更高。 // getFinalPrice(price){ // return '¥'+price.toFixed(2) // }, increment(index){ this.books[index].count++ }, decrement(index){ this.books[index].count-- }, removeHandle(index){ this.books.splice(index,1); } }, computed: { totalPrice(){ // 方案一:普通的for循環 // let totalPrice = 0; // for(let i=0;i<this.books.length;i++){ // totalPrice += this.books[i].price * this.books[i].count // } // return totalPrice // 方案二:for in // let totalPrice = 0; // for(let i in this.books){ // // console.log(i);//1 2 3 4 // totalPrice += this.books[i].price * this.books[i].count // } // return totalPrice // 方案三:for of // let totalPrice = 0; // for(let item of this.books){ // // console.log(item);//這里拿到的就是數組里的每個對象 // totalPrice += item.price * item.count // } // return totalPrice // 方案四:reduce return this.books.reduce(function (preValue, book) {// console.log(book);//分別輸出四個對象return preValue + book.price * book.count }, 0) } }, // 過濾器 filters:{ showPrice(price){ return '¥'+price.toFixed(2) } }})

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Vue
相關文章:
主站蜘蛛池模板: 成人精品福利 | 天天舔天天干 | 久久av片| 国产色网站 | 国产精品久久网 | 激情视频一区 | 久久五月婷 | 精品少妇3p| 国产特级黄色片 | 黄色a毛片 | 色妞网| 久久久久久久国产 | 日韩精品一区二区三区免费视频 | 69视频网站 | 成人免费看片98欧美 | 日韩在线不卡视频 | 成人久久视频 | 国产精品久久久久久久久久久久午夜片 | 四虎8848精品成人免费网站 | 国产精品1区 | 欧美日本国产 | 亚洲天堂免费 | 神马香蕉久久 | 欧美三根一起进三p | 一区二区三区四区精品 | 中文在线字幕免费观 | 欧美三级 欧美一级 | 亚洲在线免费视频 | 亚洲三区在线观看 | 免费在线观看黄色片 | 中文字幕免费高清 | 亚洲欧美在线视频 | 国内自拍偷拍视频 | 久久天堂av| 少妇一级淫片免费放 | 亚洲第一毛片 | 日韩精品片 | 成人免费视屏 | 国产一区二区不卡 | 欧美午夜精品一区二区三区 | 在线观看黄色片 |