vant實(shí)現(xiàn)購物車功能
做一些電商或者支付頁面,肯定少不了購物車功能,一方面正反選,另一方面動(dòng)態(tài)價(jià)格,全選之后再去加減商品數(shù)量(這里必須考慮 里面有很多蛋疼的問題)猛的一想,感覺思路很清晰,但是,真正動(dòng)起手來就各種bug出來了,說實(shí)話 搞這個(gè)購物車,浪費(fèi)我整整一下午的時(shí)間,當(dāng)我回過頭捋一遍,其實(shí),半小時(shí)就能完事。就是因?yàn)槿x的時(shí)候 我又去更改商品數(shù)量,然后調(diào)用算價(jià)格的方法導(dǎo)致浪費(fèi)了太多時(shí)間,話不多少,還是先上圖吧
先看看需求文檔吧


代碼格式不是很整齊 編輯器沒有調(diào)整 湊合看吧
<template>
<div class="pddingTop">
<van-nav-bar title='購物車' left-text="" fixed></van-nav-bar>
<div class="shopContent">
<ul>
<li v-for="(item,i) in dataList" :key="i" >
<div class="shopmain">
<van-checkbox v-model="item.checked" @change="signchecked(item)"></van-checkbox>
<div class="shops" @click="todetails(item.productCode)">
<div class="shopImg"><img :src="item.productUrl" alt=""></div>
<div class="shopsright">
<h4>{{item.productName}}</h4>
<div class="shoprightbot">
<span>¥{{item.price}}</span>
<div class="shopradd">
<button @click.stop="reducebuyNum(item.buyNum,item,item.productCode,i)">-</button>
<van-field style="width:40px;text-align: center" readonly v-model="item.buyNum" type="number" />
<button id="button" @click.stop="addbuyNum(item.buyNum,item)">+</button>
</div>
</div>
</div>
</div>
</div>
</li>
</ul>
</div>
<div v-show="!dataList.length" class="shopping">
<div><img src="./images/shopping.png" alt=""></div>
<p>暫無商品</p>
</div>
<div>
<van-submit-bar
:price="total*100"
button-text="提交訂單"
@submit="onSubmit"
>
<van-checkbox v-model="ischecked" disabled="disabled" @click="checkAll">全選</van-checkbox>
</van-submit-bar>
</div>
</div>
</template>
<script>
//toast 我全局引用了
import {Checkbox, SubmitBar,Card ,Field,Cell,Dialog, Toast } from 'vant';
import utils from '../utils/utils' //這里是自己封裝的方法 獲取
export default {
components:{
[SubmitBar.name]:SubmitBar,
[Checkbox.name]:Checkbox,
[Card.name]:Card,
[Field.name]:Field,
[Cell.name]:Cell,
},
data(){
return{
img:require("./images/gouwuche.png"),
ischecked:false,
dataList:[],
total:0,
disabled:false,
}
},
methods:{
todetails(productCode){
this.$router.push('/commodityDetails?productCode='+productCode)
},
//商品加加
addbuyNum(num,value){
if(value.buyNum<=98){
value.buyNum++
this.shopNumber(value)
this.amount(value)
}
},
//商品減減
reducebuyNum(num,value,productCode,i){
if(value.buyNum<=1){
Dialog.confirm({
title: '溫馨提示',
message: '是否刪除商品'
}).then(() => {
this.https('后臺(tái)接口',{productCode:productCode})
.then(data=>{
Toast({
message:'刪除成功',
duration:800
})
})
setTimeout(()=>{
//這里千萬不能再調(diào)用 要把這個(gè)數(shù)組里面去除掉 不然問題是很多
this.dataList.splice(i,1)
this.amount(value)
},1000)
}).catch(() => {
});
}else{
value.buyNum--
this.shopNumber(value)
this.amount(value)
}
},
// 提交訂單
onSubmit(){
let cartIdList = []
this.dataList.forEach(element => {
if (element.checked) {
cartIdList.push(String(element.dataId))
}
});
if (cartIdList.length<1) {
Toast.fail('請(qǐng)選擇訂單');
} else {
utils.putlocal('cartIdList',JSON.stringify(cartIdList))
this.$router.push('/placeorder');
}
},
//加減 這里之前是自己手寫了 但是參考別的網(wǎng)站后 這里是通過接口加減的
shopNumber(value){
let data = {
dataId :value.dataId,
buyNum:value.buyNum,
productCode:value.productCode
}
this.https('后臺(tái)接口',data)
.then(data=>{
})
},
// 單選
signchecked(val){
this.amount(val)
},
amount(val){
let arr =[]
let MoneyList=[]
this.dataList.forEach(item=>{
if(item.checked===true){
MoneyList.push(item)
arr.push(item.checked)
}
})
//這里就是判斷時(shí)候?yàn)槿x
if(arr.length===this.dataList.length){
this.ischecked = true
}else{
this.ischecked = false
}
//價(jià)格要置為0 不然一直會(huì)累加的 也會(huì)又很大的問題
this.total=0;
for(var i=0;i<MoneyList.length;i++){
this.total+=MoneyList[i].price*MoneyList[i].buyNum
}
},
// 全選 這里的事件有兩中 一個(gè)是click 一個(gè)是change 不同的事件用不同的方法
checkAll(){
this.dataList.forEach(item=>{
if(this.ischecked==false){
item.checked=true
}else{
item.checked=false
}
})
},
// 列表
shoppingCartlist () {
this.https('后臺(tái)接口',{})
.then(data=>{
if(data.code=='success'){
//這里需要手動(dòng)添加默認(rèn)的checked 后臺(tái)沒有返
data.data.forEach(element => {
element.checked = false
element.num = null
});
this.dataList = data.data
if(!this.dataList.length){
this.disabled=true
}
}else {
Toast.fail(data.message);
}
})
}
},
mounted () {
this.shoppingCartlist ()
}
}
</script>
<style lang="less" scoped>
.van-submit-bar{
bottom:49px;
padding-left:20px;
}
.shopContent{
margin-top:18px;
padding-bottom:90px;
}
.shopping{
text-align: center;
padding-top:99px;
img{
width:96px;height:96px;
margin-bottom: 25px;
}
}
li{
padding:0 15px;
background:#ffffff;
margin-bottom:10px;
position: relative;
height:103px;
.shopmain{
display: flex;
padding:10px 8px 10px 10px;
position: relative;
.shops{
display: flex;
.shopImg{
width:103px;height:83px;
margin:0 7px 0 11px;
img{
width:100%;height:100%;
}
}
.shopsright{
width:185px;
display: flex;
flex-direction:column;
justify-content: space-between;
h4{
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
.shoprightbot{
display: flex;
justify-content: space-between;
align-items: center;
width: 190px;
span{
font-size:17px;
color:#F74022;
}
}
}
}
.van-checkbox__icon--checked .van-icon{
background:red !important;
}
}
button{
width:24px;height:26px;
font-size:20px;
background:#F74022;
color:#ffffff;
border:none;
}
input{
width:48px;
}
}
.shopradd{
width: 98px;
display: flex;
.van-field__control{
text-align: center !important;
}
}
.van-cell{
padding: 0;
line-height: 26px
}
.van-field__control{
height: 26px;
}
</style>
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue的異步組件加載的實(shí)現(xiàn)方法小結(jié)
在 Vue 中,異步組件是指那些在需要時(shí)才會(huì)被加載的組件,與傳統(tǒng)的靜態(tài)組件不同,異步組件通過動(dòng)態(tài)加載的方式,可以有效地減少初始加載的資源和時(shí)間,在 Vue 中實(shí)現(xiàn)異步組件加載非常簡單,開發(fā)者有多種方法可以做到,本文給大家介紹了一些常用的實(shí)現(xiàn)方法2024-11-11
超詳細(xì)的5個(gè)Shell腳本實(shí)例分享(值得收藏)
這篇文章主要介紹了超詳細(xì)的5個(gè)Shell腳本實(shí)例分享(值得收藏),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
ElementUI+命名視圖實(shí)現(xiàn)復(fù)雜頂部和左側(cè)導(dǎo)航欄
本文主要介紹了ElementUI+命名視圖實(shí)現(xiàn)復(fù)雜頂部和左側(cè)導(dǎo)航欄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04
vue2封裝webSocket的實(shí)現(xiàn)(開箱即用)
在Vue2中,可以使用WebSocket實(shí)時(shí)通信,本文主要介紹了vue2封裝webSocket的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
vue項(xiàng)目在IE瀏覽器下運(yùn)行空白問題及解決
IE11瀏覽器無法解析ES6語法導(dǎo)致Vue項(xiàng)目在IE11下顯示空白,解決方法包括安裝babel-polyfill,并在項(xiàng)目的main.js文件中引入babel-polyfill,此外,js-base64版本3及以上不兼容IE11,解決辦法是使用版本3以下的js-base64,這些措施可以幫助兼容IE11,確保項(xiàng)目正常運(yùn)行2024-09-09

