vue實現(xiàn)書籍購物車功能
更新時間:2021年10月27日 10:58:55 作者:今天會下雨嗎
這篇文章主要為大家詳細介紹了vue實現(xiàn)書籍購物車功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)書籍購物車功能的具體代碼,供大家參考,具體內(nèi)容如下
效果圖
點擊增加、減少購買數(shù)量和移除總價格會變化

代碼
<!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>書籍購物車</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>價格</th>
<th>購買數(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時按鈕禁用 -->
<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>總價格:{{totalPrice | showPrice}}</h2>
</div>
<h2 v-else>購物車為空</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:{
// 寫在計算屬性里的方法可以直接當(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. 步驟稍簡單的普通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);
},
// 過濾器
filters:{
showPrice(price){
return "¥" + price.toFixed(2);
}
}
})
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vee-validate vue 2.0自定義表單驗證的實例
今天小編就為大家分享一篇vee-validate vue 2.0自定義表單驗證的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vue-admin-template?動態(tài)路由的實現(xiàn)示例
本文主要介紹了ue-admin-template動態(tài)路由的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-12-12
Vue組件傳值異步問題子組件拿到數(shù)據(jù)較慢解決
這篇文章主要為大家介紹了Vue組件傳值異步中子組件拿到數(shù)據(jù)較慢的問題解決方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-08-08
vue?cesium加載點與定位到指定位置的實現(xiàn)方法
Cesium是一個用于創(chuàng)建高性能、跨平臺的3D地球和地圖的開源JavaScript庫,它提供了許多功能,包括地理空間數(shù)據(jù)可視化、地理定位和地圖導(dǎo)航等,這篇文章主要介紹了vue?cesium加載點與定位到指定位置,需要的朋友可以參考下2024-03-03
vxe-table?實現(xiàn)?excel?選擇一個單元格拖拽自動復(fù)制新的單元格(示例代碼)
vxe-table是一款強大的表格組件,支持Excel風(fēng)格的操作,通過鼠標右下角的擴展按鈕,用戶可以拖拽選擇單元格并自動復(fù)制內(nèi)容到擴展區(qū)域的所有單元格中,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2025-01-01
vue 監(jiān)聽input輸入事件(oninput)的示例代碼支持模糊查詢
這篇文章主要介紹了vue 監(jiān)聽input輸入事件(oninput)支持模糊查詢,比如說表格模糊查詢,實現(xiàn)一邊輸入,一邊過濾數(shù)據(jù),本文通過示例代碼給大家詳細講解,需要的朋友可以參考下2023-02-02

