Vue 購(gòu)物車(chē)案例練習(xí)
1.購(gòu)物車(chē)案例
經(jīng)過(guò)一系列的學(xué)習(xí),我們這里來(lái)練習(xí)一個(gè)購(gòu)物車(chē)的案例
需求:使用
vue寫(xiě)一個(gè)表單頁(yè)面,頁(yè)面上有購(gòu)買(mǎi)的數(shù)量,點(diǎn)擊按鈕+或者-,可以增加或減少購(gòu)物車(chē)的數(shù)量,數(shù)量最少不得少于0,點(diǎn)擊移除按鈕,會(huì)移除該商品,當(dāng)把所有的商品移除后,頁(yè)面上的表單消失,然后出現(xiàn)文字:購(gòu)物車(chē)為空,表單下方是商品的總價(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>書(shū)籍名稱(chēng)</th>
<th>出版日期</th>
<th>價(jià)格</th>
<th>購(gòu)買(mǎi)數(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)物車(chē)為空</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)物車(chē)數(shù)量≤0,則無(wú)法點(diǎn)擊
v-if和v-else:條件判斷,判斷books的列表長(zhǎng)度,如果有長(zhǎng)度展示列表,如果長(zhǎng)度為0則展示文字購(gòu)物車(chē)為空
filters:自定義過(guò)濾器,過(guò)濾價(jià)格,使本身的價(jià)格過(guò)濾后帶有2位小數(shù)
computed:計(jì)算屬性,計(jì)算購(gòu)物的總價(jià)格
到此這篇關(guān)于Vue 購(gòu)物車(chē)案例練習(xí)的文章就介紹到這了,更多相關(guān)Vue 購(gòu)物車(chē)練習(xí)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js學(xué)習(xí)筆記之v-bind和v-on解析
這篇文章主要介紹了vue.js學(xué)習(xí)筆記之v-bind和v-on解析,v-bind 指令用于響應(yīng)地更新 HTML 特征,v-on 指令用于監(jiān)聽(tīng)DOM事件,文中還給大家提到了v-bind,v-on的縮寫(xiě),感興趣的朋友參考下吧2018-05-05
VUEJS實(shí)戰(zhàn)之修復(fù)錯(cuò)誤并且美化時(shí)間(2)
這篇文章主要為大家詳細(xì)介紹了VUEJS實(shí)戰(zhàn)之修復(fù)錯(cuò)誤并且美化時(shí)間,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
vue在路由中驗(yàn)證token是否存在的簡(jiǎn)單實(shí)現(xiàn)
今天小編就為大家分享一篇vue在路由中驗(yàn)證token是否存在的簡(jiǎn)單實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue+axios實(shí)現(xiàn)圖片上傳識(shí)別人臉的示例代碼
本文主要介紹了vue+axios實(shí)現(xiàn)圖片上傳識(shí)別人臉,這里采用的是vant的文件上傳組件,通過(guò)上傳圖片后端識(shí)別圖片里的人臉,感興趣的可以了解一下2021-11-11
Vue v-for中:key中item.id與Index使用的區(qū)別解析
這篇文章主要介紹了Vue v-for中:key中item.id與Index使用的區(qū)別解析,推薦使用【:key="item.id"】而不是將數(shù)組下標(biāo)當(dāng)做唯一標(biāo)識(shí),前者能做到全部復(fù)用,本文給大家詳細(xì)講解,感興趣的朋友跟隨小編一起看看吧2024-02-02
解決vue admin element noCache設(shè)置無(wú)效的問(wèn)題
今天小編就為大家分享一篇解決vue admin element noCache設(shè)置無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11

