Vuex實(shí)現(xiàn)購物車小功能
Vuex實(shí)現(xiàn)購物車功能(附:效果視頻),供大家參考,具體內(nèi)容如下
功能描述:
- 加購
- 刪除
- 加減
- 全選反選
- 選中計(jì)算總價(jià)
- 存儲(chǔ)
整體演示效果如下:

首先介紹一下Vuex:
Vuex 實(shí)例對(duì)象屬性 主要有下面5個(gè)核心屬性:
state : 全局訪問的state對(duì)象,存放要設(shè)置的初始狀態(tài)名及值(必須要有)
mutations : 里面可以存放改變 state 的初始值的方法 ( 同步操作–必須要有 )
getters: 實(shí)時(shí)監(jiān)聽state值的變化可對(duì)狀態(tài)進(jìn)行處理,返回一個(gè)新的狀態(tài),相當(dāng)于store的計(jì)算屬性(不是必須的)
actions : 里面可以存放用來異步觸發(fā) mutations 里面的方法的方法 ( 異步操作–不是必須的 )
modules : 存放模塊化的數(shù)據(jù)(不是必須的)
一、主頁面Home:
<template>
<div id="app">
<div class="nav-top">
<!-- 標(biāo)簽欄-->
<van-nav-bar
title="商品列表頁"
left-arrow
/>
</div>
<div class="nav-bottom">
<!-- 商品卡片-->
<van-card
v-for="item in goodsList"
:price="item.actualPrice"
:desc="item.desc"
:title="item.dtitle"
:thumb="item.mainPic"
>
<template #num>
<van-icon name="shopping-cart-o" color="red" size="24px" @click="add(item)"/>
</template>
</van-card>
</div>
</div>
</template>
<script>
export default {
data() {
return {
goodsList: [], // 商品列表數(shù)據(jù)
}
},
// 請(qǐng)求商品列表數(shù)據(jù)
mounted() {
// 接口不予展示,有需要請(qǐng)自行下載
this.$axios.get('api接口').then(res => {
this.goodsList = res.data.data.data
// console.log(this.goodsList)
})
},
methods: {
// 添加商品 調(diào)用vuex中的add方法
add(item) {
this.$store.commit('add', item)
}
}
}
</script>
<style lang="scss" scoped>
#app {
.nav-top {
width: 100%;
position: fixed;
top: 0;
left: 0;
z-index: 9;
}
.nav-bottom {
margin-top: 50px;
}
}
</style>
二、購物車頁面ShopCart:
<template>
<div>
<!-- 標(biāo)簽欄-->
<van-nav-bar
title="購物車"
left-arrow
@click-left="back"
/>
<!-- 購物車box -->
<div class="car-box" v-for="(item,index) in list" :key="index">
<!-- 左側(cè)復(fù)選框布局-->
<div class="car-box-left">
<van-checkbox v-model="item.ckd"></van-checkbox>
</div>
<!-- 右側(cè)商品卡片布局-->
<div class="car-box-right">
<van-swipe-cell>
<van-card
:price="item.item.actualPrice"
:title="item.item.dtitle"
:thumb="item.item.mainPic"
>
<!-- 步進(jìn)器-->
<template #num>
<van-stepper v-model="item.num" theme="round" button-size="22" disable-input
@change="changeNum(item.num)"/>
</template>
</van-card>
<!-- 右側(cè)滑動(dòng)刪除-->
<template #right>
<van-button square text="刪除" type="danger" class="delete-button" @click="del(index)"/>
</template>
</van-swipe-cell>
</div>
</div>
<!-- 空狀態(tài) 沒數(shù)據(jù)顯示 有數(shù)據(jù)隱藏-->
<van-empty
v-show="$store.state.cartList.length==0"
class="custom-image"
image="https://img.yzcdn.cn/vant/custom-empty-image.png"
description="購物車是空的喲!"
/>
<!-- 商品導(dǎo)航-->
<van-submit-bar :price="$store.getters.total*100" button-text="提交訂單">
<van-checkbox v-model="checkAll">全選</van-checkbox>
</van-submit-bar>
</div>
</template>
<script>
import {mapMutations} from 'vuex'
export default {
data() {
return {
list: this.$store.state.cartList, //購物車數(shù)據(jù)
}
},
computed: {
// 計(jì)算屬性checkAll 和全選按鈕雙向數(shù)據(jù)綁定,別人可以控制它 它也可以控制別人
// 別人控制它 給他一個(gè)值的時(shí)候是 get , 它控制別人 給別人設(shè)置值的時(shí)候 是set
// 在set中newVal參數(shù)是這個(gè)計(jì)算屬性改變后的值
checkAll: { //可以看作一個(gè)事件
get() {
// 判斷購物車?yán)锷唐返拈L度為0 return false
if (this.list.length == 0) {
return false
}
return this.$store.state.cartList.every(item => {
return item.ckd == true // 返回結(jié)果復(fù)選框?yàn)閠rue
})
},
set(newVal) {
this.$store.commit('ckd', newVal)
},
}
},
methods: {
// 返回商品列表頁
back() {
this.$router.push({
path: '/'
})
},
//方法集合
...mapMutations(['del', 'changeNum',])
},
}
</script>
<style lang="scss" scoped>
.goods-card {
margin: 0;
background-color: white;
}
.delete-button {
height: 100%;
}
.car-box {
width: 100%;
margin-bottom: 5px;
display: flex;
flex-wrap: nowrap;
align-items: center;
.car-box-left {
flex: 1;
padding-left: 10px;
}
.car-box-right {
flex: 12;
}
}
</style>
三、Vuex代碼:
import Vue from 'vue'
import Vuex from 'vuex'
import VuexPersist from 'vuex-persist'
Vue.use(Vuex);
export default new Vuex.Store({
state: {
cartList: [], // 購物車數(shù)據(jù)
},
mutations: {
// 添加商品
add(state, item) {
// console.log(item)
let flag = false;
// 加購去重(通過id判斷)
state.cartList.forEach(i => {
if (i.item.id == item.id) {
i.num++;
flag = true;
}
})
if (flag == false) {
state.cartList.push({
num: 1, // 添加數(shù)量默認(rèn)為1
item, // 添加購物車商品數(shù)據(jù)
ckd: false, // 添加復(fù)選框初始化狀態(tài)為false
})
}
// console.log(state.cartList)
},
// 刪除商品
del(state, index) {
state.cartList.splice(index, 1)
// state.
},
// 改變數(shù)量------加減綜合法 !??!
changeNum(state, index) {
state.cartList.num = index
},
// 全選全不選
ckd(state, newAll) {
state.cartList.forEach(item => {
item.ckd = newAll
})
}
},
// 計(jì)算 getters
getters: {
// 總價(jià)
total(state) {
let sum = 0;
state.cartList.forEach(item => {
// 如果復(fù)選框選中 計(jì)算總價(jià)
if (item.ckd == true) {
sum += item.item.actualPrice * item.num
}
})
return sum
}
},
actions: {},
modules: {},
// Vuex存儲(chǔ)插件
plugins: [
new VuexPersist({
storage: window.localStorage,
}).plugin,
]
})
關(guān)于vue.js組件的教程,請(qǐng)大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vite+Vue3使用MockJS的實(shí)現(xiàn)示例
寫一些純前端的項(xiàng)目時(shí),自己造數(shù)據(jù)有些麻煩,于是我們可以利用mock造一些簡單的數(shù)據(jù),來滿足我們的需求,本文主要介紹了Vite+Vue3使用MockJS的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-01-01
在Vant的基礎(chǔ)上實(shí)現(xiàn)添加表單驗(yàn)證框架的方法示例
這篇文章主要介紹了在Vant的基礎(chǔ)上實(shí)現(xiàn)添加驗(yàn)證框架的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-12-12
vue使用formData時(shí)候傳遞參數(shù)是個(gè)空值的情況處理
這篇文章主要介紹了vue使用formData時(shí)候傳遞參數(shù)是個(gè)空值的情況處理,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
解決vue中axios設(shè)置超時(shí)(超過5分鐘)沒反應(yīng)的問題
這篇文章主要介紹了解決vue中axios設(shè)置超時(shí)(超過5分鐘)沒反應(yīng)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09
在VUE中實(shí)現(xiàn)文件下載并判斷狀態(tài)的方法
今天小編就為大家分享一篇在VUE中實(shí)現(xiàn)文件下載并判斷狀態(tài)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
vant使用datetime-picker組件設(shè)置maxDate和minDate的坑及解決
這篇文章主要介紹了vant使用datetime-picker組件設(shè)置maxDate和minDate的坑及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12

