国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

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

Vue實現小購物車功能

瀏覽:5日期:2022-10-17 14:14:44

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

本人還在初級學習階段,有很多不足之處,希望能指出錯誤,一起進步

HTML代碼塊

<body> <div id='app'> <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='(itme,index) in books'> <td>{{itme.id}}</td> <td>{{itme.name}}</td> <td>{{itme.date}}</td> <!-- showPrice過濾器 --> <td>{{itme.price | showPrice}}</td> <td> <!-- 動態綁定disabled,當數量小于1時 禁止點擊按鈕--> <button @click='decrement(index)' :disabled='itme.count <= 1'>-</button> {{itme.count}} <button @click='increment(index)'>+</button> </td> <td><button @click='Handle(index)'>移除</button></td> </tr> </tbody> </table> <h2>總價格:{{totalPrice | showPrice}}</h2> </div> <h2 v-else>購物車為空</h2> </div></body>

css代碼塊

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: #f7f7f7; color: #5c6b77; font-weight: 600;}

Vue.js代碼塊

const app = new Vue({ el:’#app’, data:{ books:[ { id:1, name:’《算法議論》’, date:’2001-1’, price:85.00, count:1 }, { id:2, name:’《編程珠璣》’, date:’2002-1’, price:65.00, count:1 }, { id:3, name:’《Unix編程藝術》’, date:’2000-1’, price:59.00, count:1 }, { id:4, name:’《代碼大全》’, date:’2005-1’, price:135.00, count:1 }, ] }, /** * 使用普通方法 */ methods:{ //獲取小數點的方法 // getFinalPrice(price){ // return ’¥’ + price.toFixed(2); // } //點擊事件 increment(index){ this.books[index].count++; }, decrement(index){ this.books[index].count--; }, Handle(index){ this.books.splice(index,1); } }, computed:{ totalPrice(){ let totalPrice = 0; for(let i = 0; i < this.books.length; i++){ totalPrice += this.books[i].price * this.books[i].count; } return totalPrice; } }, /** * 使用過濾器獲取小數點 */ filters:{ showPrice(price){ return ’¥’ + price.toFixed(2); } }})

效果如圖:

Vue實現小購物車功能

還有很多不足之處,希望大家能指出。

關于vue.js的學習教程,請大家點擊專題vue.js組件學習教程、Vue.js前端組件學習教程進行學習。

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

標簽: Vue
相關文章:
主站蜘蛛池模板: 永德县| 武乡县| 北碚区| 岳阳县| 精河县| 武定县| 迁西县| 梅州市| 襄城县| 简阳市| 禄劝| 金华市| 泽州县| 崇信县| 三江| 平昌县| 赤城县| 边坝县| 福安市| 清流县| 鸡泽县| 固始县| 运城市| 郴州市| 呼和浩特市| 博白县| 新乐市| 江门市| 平泉县| 庆城县| 达州市| 蓝田县| 瑞安市| 嘉黎县| 盐山县| 万安县| 威宁| 绥滨县| 成安县| 福安市| 湘潭县|