Vue 購(gòu)物車案例練習(xí)
1.購(gòu)物車案例
經(jīng)過(guò)一系列的學(xué)習(xí),我們這里來(lái)練習(xí)一個(gè)購(gòu)物車的案例
需求:使用
vue寫一個(gè)表單頁(yè)面,頁(yè)面上有購(gòu)買的數(shù)量,點(diǎn)擊按鈕+或者-,可以增加或減少購(gòu)物車的數(shù)量,數(shù)量最少不得少于0,點(diǎn)擊移除按鈕,會(huì)移除該商品,當(dāng)把所有的商品移除后,頁(yè)面上的表單消失,然后出現(xiàn)文字:購(gòu)物車為空,表單下方是商品的總價(jià)格,隨著商品的數(shù)量增加而增加,默認(rèn)是0元,
總體效果如下:

2.代碼實(shí)現(xiàn)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="../js/vue.js"></script>
<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">
<div v-if="books.length">
<table>
<thread>
<tr>
<th></th>
<th>書籍名稱</th>
<th>出版日期</th>
<th>價(jià)格</th>
<th>購(gòu)買數(shù)量</th>
<th>操作</th>
</tr>
</thread>
<tbody>
<tr v-for="(book, index) in books" :key="book">
<td>{{index+1}}</td>
<td>{{book.name}}</td>
<td>{{book.publish_date}}</td>
<td>{{book.price | showPrice}}</td>
<td>
<button @click="decrease(index)" :disabled="book.count <= 0">-</button>
{{book.count}}
<button @click="increase(index)">+</button>
</td>
<td>
<button @click="removeClick(index)">移除</button>
</td>
</tr>
</tbody>
</table>
<p>總價(jià):{{totalPrice | showPrice}}</p>
</div>
<h2 v-else>購(gòu)物車為空</h2>
</div>
<script>
const app = new Vue({
el: "#app",
data: {
books: [
{"name":"算法導(dǎo)論", "publish_date":"2006-9", "price":20.00, "count": 0},
{"name":"UNIX編程藝術(shù)", "publish_date":"2006-2", "price":30.00, "count": 0},
{"name":"編程技術(shù)", "publish_date":"2008-10", "price":40.00, "count": 0},
{"name":"代碼大全", "publish_date":"2006-3", "price":50.00, "count": 0},
],
},
methods: {
// 增加+
decrease(index){
this.books[index].count-=1
},
// 減少-
increase(index){
this.books[index].count+=1
},
// 移除按鈕
removeClick(index){
this.books.splice(index, 1)
}
},
computed: {
// 計(jì)算總價(jià)格
totalPrice(){
let totalPrice = 0
for (let item of this.books){
totalPrice += item.price * item.count
}
return totalPrice
}
},
// 過(guò)濾器,將價(jià)格過(guò)濾成有2位小數(shù)的
filters: {
showPrice(price){
return '¥' + price.toFixed(2)
}
}
})
</script>
</body>
</html>
3.總結(jié)
v-for:循環(huán),循環(huán)books列表
v-on:事件監(jiān)聽(tīng),監(jiān)聽(tīng)點(diǎn)擊事件
disabled:按鈕是否可以點(diǎn)擊的屬性,為True可點(diǎn)擊,為False不可點(diǎn)擊,增加判斷條件:disabled="book.count <= 0"當(dāng)購(gòu)物車數(shù)量≤0,則無(wú)法點(diǎn)擊
v-if和v-else:條件判斷,判斷books的列表長(zhǎng)度,如果有長(zhǎng)度展示列表,如果長(zhǎng)度為0則展示文字購(gòu)物車為空
filters:自定義過(guò)濾器,過(guò)濾價(jià)格,使本身的價(jià)格過(guò)濾后帶有2位小數(shù)
computed:計(jì)算屬性,計(jì)算購(gòu)物的總價(jià)格
到此這篇關(guān)于Vue 購(gòu)物車案例練習(xí)的文章就介紹到這了,更多相關(guān)Vue 購(gòu)物車練習(xí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解lottie動(dòng)畫在vue中的應(yīng)用
Lottie 是一個(gè)適用于 Android、iOS、Web 和 Windows 的庫(kù),它使用 Bodymovin 解析導(dǎo)出為 JSON 的 Adobe After Effects 動(dòng)畫,下面我們就來(lái)看看它在vue中的是如何應(yīng)用的吧2023-12-12
vue回到頂部監(jiān)聽(tīng)滾動(dòng)事件詳解
這篇文章主要為大家詳細(xì)介紹了vue回到頂部監(jiān)聽(tīng)滾動(dòng)事件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
解決ant-design-vue中menu菜單無(wú)法默認(rèn)展開(kāi)的問(wèn)題
這篇文章主要介紹了解決ant-design-vue中menu菜單無(wú)法默認(rèn)展開(kāi)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10
vue的diff算法知識(shí)點(diǎn)總結(jié)
本篇文章給大家分享了關(guān)于vue的diff算法的相關(guān)知識(shí)點(diǎn)總結(jié),有興趣的朋友參考學(xué)習(xí)下。2018-03-03
vxe-list?vue?如何實(shí)現(xiàn)下拉框的虛擬列表
這篇文章主要介紹了vxe-list?vue?如何實(shí)現(xiàn)下拉框的虛擬列表,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
axios取消請(qǐng)求CancelToken的用法示例代碼
Axios提供了取消請(qǐng)求的功能,可以通過(guò)使用CancelToken來(lái)實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于axios取消請(qǐng)求CancelToken的用法,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10
詳解如何在Vue2中實(shí)現(xiàn)組件props雙向綁定
在Vue2中組件的props的數(shù)據(jù)流動(dòng)改為了只能單向流動(dòng),如何在Vue2中實(shí)現(xiàn)組件props雙向綁定 ,一起來(lái)跟小編看看。2017-03-03
vue中簡(jiǎn)單彈框dialog的實(shí)現(xiàn)方法
下面小編就為大家分享一篇vue中簡(jiǎn)單彈框dialog的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-02-02
Vue實(shí)現(xiàn)監(jiān)聽(tīng)dom節(jié)點(diǎn)寬高變化方式
這篇文章主要介紹了Vue實(shí)現(xiàn)監(jiān)聽(tīng)dom節(jié)點(diǎn)寬高變化方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10

