vue實現(xiàn)可改變購物數(shù)量的購物車
更新時間:2021年07月19日 14:11:04 作者:yfy538
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)可改變購物數(shù)量的購物車,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了vue實現(xiàn)改變購物數(shù)量的購物車,供大家參考,具體內(nèi)容如下
效果圖:

知識點:
1.computed 計算屬性
2.filters 過濾器
實現(xiàn)代碼:
<!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>Document</title>
</head>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<style>
table {
border-collapse: collapse;
border-spacing: 0;
border: 1px solid #ccc;
}
td,
th {
padding: 8px 16px;
border: 1px solid #ccc;
text-align: left;
}
th {
background-color: #f7f7f7;
color: #5c6b77;
}
</style>
<body>
<div id="box">
<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>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.date}}</td>
<td>{{item.price | toprice}}</td>
<td>
<button @click='down(index)' :disabled="item.aunt<=1">-</button> {{item.aunt}}
<button @click='add(index)'>+</button>
</td>
<td>
<button @click="remove(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<h2>總價:{{getallprice | toprice}}</h2>
</div>
<h2 v-else>您沒有購物信息</h2>
</div>
<script>
const vm = new Vue({
el: "#box",
data: {
books: [{
id: 1,
name: "《vue.js實戰(zhàn)》",
date: "2010.2.4",
price: 82.00,
aunt: 1
}, {
id: 2,
name: "《javascript實戰(zhàn)》",
date: "2010.2.4",
price: 108.00,
aunt: 1
}, {
id: 3,
name: "《html+css實戰(zhàn)》",
date: "2010.2.4",
price: 42.50,
aunt: 1
}, {
id: 4,
name: "《axios實戰(zhàn)》",
date: "2010.2.4",
price: 82.00,
aunt: 1
}, {
id: 5,
name: "《jquery實戰(zhàn)》",
date: "2010.2.4",
price: 65.20,
aunt: 1
}, ]
},
methods: {
add(index) {
this.books[index].aunt++;
},
down(index) {
this.books[index].aunt--;
},
remove(index) {
this.books.splice(index, 1)
},
},
computed: {
getallprice() {
let all = 0;
for (let i = 0; i < this.books.length; i++) {
all += this.books[i].price * this.books[i].aunt
}
return all
}
},
filters: {
toprice(price) {
return '¥' + price.toFixed(2)
},
}
})
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
關(guān)于echarts?清空上一次加載的數(shù)據(jù)問題
這篇文章主要介紹了關(guān)于echarts?清空上一次加載的數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
element?ui富文本編輯器的使用效果與步驟(quill-editor)
富文本編輯器在任何項目中都會用到,在Element中我們推薦vue-quill-editor組件,下面這篇文章主要給大家介紹了關(guān)于element?ui富文本編輯器的使用效果與步驟(quill-editor)的相關(guān)資料,需要的朋友可以參考下2022-10-10
Vue3+antDesignVue實現(xiàn)表單校驗的方法
這篇文章主要為大家詳細(xì)介紹了基于Vue3和antDesignVue實現(xiàn)表單校驗的方法,文中的示例代碼講解詳細(xì),具有一定的參考價值,需要的小伙伴可以了解下2024-01-01
vue通過子組件修改父組件prop的多種實現(xiàn)方式
這篇文章主要介紹了vue通過子組件修改父組件prop的幾種實現(xiàn)方式,比較常用的方式是通過Prop單向傳遞的規(guī)則,需要的朋友可以參考下2021-09-09
Vue使用axios進(jìn)行g(shù)et請求拼接參數(shù)的2種方式詳解
axios中post請求都是要求攜帶參數(shù)進(jìn)行請求,這篇文章主要給大家介紹了關(guān)于Vue使用axios進(jìn)行g(shù)et請求拼接參數(shù)的2種方式,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01

