vue實(shí)現(xiàn)可改變購(gòu)物數(shù)量的購(gòu)物車
本文實(shí)例為大家分享了vue實(shí)現(xiàn)改變購(gòu)物數(shù)量的購(gòu)物車,供大家參考,具體內(nèi)容如下
效果圖:

知識(shí)點(diǎn):
1.computed 計(jì)算屬性
2.filters 過(guò)濾器
實(shí)現(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>價(jià)格</th>
<th>購(gòu)買數(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>總價(jià):{{getallprice | toprice}}</h2>
</div>
<h2 v-else>您沒有購(gòu)物信息</h2>
</div>
<script>
const vm = new Vue({
el: "#box",
data: {
books: [{
id: 1,
name: "《vue.js實(shí)戰(zhàn)》",
date: "2010.2.4",
price: 82.00,
aunt: 1
}, {
id: 2,
name: "《javascript實(shí)戰(zhàn)》",
date: "2010.2.4",
price: 108.00,
aunt: 1
}, {
id: 3,
name: "《html+css實(shí)戰(zhàn)》",
date: "2010.2.4",
price: 42.50,
aunt: 1
}, {
id: 4,
name: "《axios實(shí)戰(zhàn)》",
date: "2010.2.4",
price: 82.00,
aunt: 1
}, {
id: 5,
name: "《jquery實(shí)戰(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vuejs手把手教你寫一個(gè)完整的購(gòu)物車實(shí)例代碼
- 基于Vuejs實(shí)現(xiàn)購(gòu)物車功能
- Vue實(shí)現(xiàn)購(gòu)物車功能
- vue實(shí)現(xiàn)購(gòu)物車小案例
- vue實(shí)現(xiàn)商城購(gòu)物車功能
- vue 實(shí)現(xiàn)購(gòu)物車總價(jià)計(jì)算
- vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車功能示例
- vue+vant-UI框架實(shí)現(xiàn)購(gòu)物車的復(fù)選框全選和反選功能
- Vue.js實(shí)現(xiàn)的購(gòu)物車功能詳解
- Vuejs實(shí)現(xiàn)購(gòu)物車功能
相關(guān)文章
Vue中常用的rules校驗(yàn)規(guī)則的實(shí)現(xiàn)
在vue開發(fā)中,難免遇到各種表單校驗(yàn),本文主要介紹了Vue中常用的rules校驗(yàn)規(guī)則的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
Vue3 使用Vuex和router的注意事項(xiàng)及操作方法
在vue2中 使用的?this.$route?和?this.$router?this.$store的使用在vue3中完全適用,這篇文章主要介紹了Vue3 使用Vuex和router的注意事項(xiàng)及操作方法,需要的朋友可以參考下2022-12-12
vue實(shí)現(xiàn)商品列表的添加刪除實(shí)例講解
在本篇內(nèi)容里小編給大家分享的是關(guān)于vue實(shí)現(xiàn)商品列表的添加刪除實(shí)例講解,有興趣的朋友們可以參考下。2020-05-05
vue實(shí)現(xiàn)綁定事件的方法實(shí)例代碼詳解
這篇文章主要介紹了vue實(shí)現(xiàn)綁定事件的方法實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
Vue項(xiàng)目Element表格對(duì)應(yīng)字段映射顯示方法:formatter格式化數(shù)據(jù)問題
這篇文章主要介紹了Vue項(xiàng)目Element表格對(duì)應(yīng)字段映射顯示方法:formatter格式化數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07
使用webpack搭建vue項(xiàng)目及注意事項(xiàng)
這篇文章主要介紹了使用webpack搭建vue項(xiàng)目的方法,本文以開發(fā)環(huán)境為例,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-06-06
vue3+element-plus 實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)路由的渲染的項(xiàng)目實(shí)踐
本文主要介紹了vue3+element-plus 實(shí)現(xiàn)動(dòng)態(tài)菜單和動(dòng)態(tài)路由的渲染的項(xiàng)目實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-11-11
Vue子組件如何修改父組件數(shù)據(jù)的方法及注意事項(xiàng)
這篇文章主要介紹了Vue子組件如何修改父組件數(shù)據(jù)的方法及注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

