午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

Vue實(shí)現(xiàn)簡易購物車案例

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

本文實(shí)例為大家分享了Vue實(shí)現(xiàn)簡易購物車的具體代碼,供大家參考,具體內(nèi)容如下

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

Vue實(shí)現(xiàn)簡易購物車案例

CSS 部分

這里沒什么好說的,就是v-cloak 這一個(gè)知識點(diǎn)

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的知識點(diǎn):

v-if v-for v-cloak v-on > @ v-bind > : 方法 methods 計(jì)算屬性 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.leifengta.com.cn/bcjs/style.css' ></head><body> <div v-cloak> <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='(item,index) in books'> <th>{{item.id}}</th> <th>{{item.name}}</th> <th>{{item.date}}</th> <!--方案一 保留小數(shù)點(diǎn)和貨幣符號--> <!-- <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>總價(jià)格:{{totalPrice | showPrice}}</h2> </div> <h2 v-else> 購物車為空 </h2> </div></body><script src='http://www.leifengta.com.cn/js/vue.js'></script><script src='http://www.leifengta.com.cn/bcjs/main.js'></script></html>JS部分

const app = new Vue({ el:'#app', data:{ books:[ {id:1,name:'《算法導(dǎo)論》',date:’2006-9’,price:85.00,count:1 }, {id:2,name:'《UNIX編程藝術(shù)》',date:’2006-2’,price:50.00,count:1 }, {id:3,name:'《編程藝術(shù)》',date:’2008-10’,price:39.00,count:1 }, {id:4,name:'《代碼大全》',date:’2006-3’,price:128.00,count:1 }, ] }, methods: { //這里我們放棄使用方法的形式來求總價(jià)格,轉(zhuǎn)而使用計(jì)算屬性,因?yàn)樗男矢摺? // 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循環(huán) // 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);//這里拿到的就是數(shù)組里的每個(gè)對象 // totalPrice += item.price * item.count // } // return totalPrice // 方案四:reduce return this.books.reduce(function (preValue, book) {// console.log(book);//分別輸出四個(gè)對象return preValue + book.price * book.count }, 0) } }, // 過濾器 filters:{ showPrice(price){ return '¥'+price.toFixed(2) } }})

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 伊人久久大 | 亚洲日本色 | 亚洲三级中文字幕 | 亚洲一区在线免费观看 | 黄色片网站免费看 | 色先锋在线 | 日批视频在线 | 欧美色视频在线观看 | 精品乱子伦一区二区 | 在线免费观看亚洲 | 性欧美videos | xxxx在线视频 | 久久国产精品久久 | 国产专区精品 | 国产一区在线观看视频 | 色先锋影音 | 四库影院在线观看 | 久草网在线视频 | 一区二区国产在线 | 手机天堂av | 美日韩在线 | 成人福利av| 婷婷中文 | 亚洲国产成人在线视频 | 亚洲的天堂 | 欧美在线国产 | 麻豆做爰免费观看 | xxxx精品 | 久久久精品网站 | 亚洲欧美另类日韩 | 国产精品主播一区二区 | 欧美精品一区二 | 国产精品一区二区久久久 | 国产操视频 | 国产高清视频在线观看 | 91精品在线观看视频 | 午夜影院黄 | 欧美精品一二三 | 你懂的在线免费观看 | 免费啪啪小视频 | 天天在线免费视频 |