vuex實(shí)現(xiàn)購物車的增加減少移除
本文實(shí)例為大家分享了vuex實(shí)現(xiàn)購物車增加減少移除的具體代碼,供大家參考,具體內(nèi)容如下
1.store.js(公共的倉庫)
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
carList: [] //購物車的商品
},
mutations: {
// 加
addCar(state, params) {
let CarCon = state.carList;
// 判斷如果購物車是否有這個商品,有就只增加數(shù)量,否則就添加一個
// some 只要有一個isHas為true,就為true
let isHas = CarCon.some((item) => {
if (params.id == item.id) {
item.num++;
return true;
} else {
return false;
}
})
if (!isHas) {
let obj = {
"id": params.id,
"title": params.title,
"price": params.price,
"num": 1,
}
this.state.carList.push(obj)
}
},
// 減
reducedCar(state,params){
let len=state.carList.length;
for(var i=0;i<len;i++){
if(state.carList[i].id==params.id){
state.carList[i].num--
if(state.carList[i].num==0){
state.carList.splice(i,1);
break;
}
}
}
},
//移出
deleteCar(state,params){
let len=state.carList.length;
for(var i=0;i<len;i++){
if(state.carList[i].id==params.id){
state.carList.splice(i,1);
break;
}
}
},
// 初始化購物車,有可能用戶一登錄直接進(jìn)入購物車
// initCar(state, car) {
// state.carList = car
// },
},
actions: {
// 加
addCar({ commit }, params) {
// console.log(params) //點(diǎn)擊添加傳過來的參數(shù)
// 使用setTimeout模擬異步獲取購物車的數(shù)據(jù)
setTimeout(function () {
let result = 'ok'
if (result == 'ok') {
// 提交給mutations
commit("addCar", params)
}
}, 100)
},
// 減
reducedCar({ commit }, params) {
// console.log(params) //點(diǎn)擊添加傳過來的參數(shù)
// 使用setTimeout模擬異步獲取購物車的數(shù)據(jù)
setTimeout(function () {
let result = 'ok'
if (result == 'ok') {
// 提交給mutations
commit("reducedCar", params)
}
}, 100)
},
// 移出
deleteCar({ commit }, params) {
// console.log(params) //點(diǎn)擊添加傳過來的參數(shù)
// 使用setTimeout模擬異步獲取購物車的數(shù)據(jù)
setTimeout(function () {
let result = 'ok'
if (result == 'ok') {
// 提交給mutations
commit("deleteCar", params)
}
}, 100)
}
// initCar({ commit }) {
// setTimeout(function () {
// let result = 'ok'
// if (result == 'ok') {
// // 提交給mutations
// commit("initCar", [{
// "id": 20193698,
// "title": '我是購物車原來的',
// "price": 30,
// "num": 100,
// }])
// }
// }, 100)
// }
},
getters: {
//返回購物車的總價
totalPrice(state) {
let Carlen = state.carList;
let money = 0;
if (Carlen.length != 0) {
Carlen.forEach((item) => {
money += item.price * item.num
})
return money;
} else {
return 0;
}
},
//返回購物車的總數(shù)
carCount(state) {
return state.carList.length
}
},
})
2. list.vue(商品列表)
<template>
<!-- 商品列表 -->
<div id="listBox">
<!-- -->
<router-link :to="{path:'/car'}" style="line-height:50px">跳轉(zhuǎn)到購物車</router-link>
<el-table :data="tableData" border style="width: 100%">
<el-table-column fixed prop="id" align="center" label="商品id"></el-table-column>
<el-table-column prop="title" align="center" label="商品標(biāo)題"></el-table-column>
<el-table-column prop="price" align="center" label="商品價格"></el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-button @click="addCar(scope.row)" type="text" size="small">加入購物車</el-button>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
name: "listBox",
data() {
return {
tableData: [] //商品列表
};
},
methods: {
// 初始化商品列表
initTable(){
this.$gAjax(`../static/shopList.json`)
.then(res => {
console.log(res)
this.tableData=res;
})["catch"](() => {});
},
// 加入購物車
addCar(row){
// console.log(row)
// 提交給store里面actions 由于加入購物車的數(shù)據(jù)要同步到后臺
this.$store.dispatch('addCar',row)
}
},
mounted () {
this.initTable()
}
};
</script>
<style>
#listBox {
width: 900px;
margin: 0 auto;
}
</style>
3. car.vue(購物車)
<template>
<!-- 購物車 -->
<div id="carBox">
<!-- 商品總數(shù) -->
<h2 style="line-height:50px;font-size:16px;font-weight:bold">合計(jì):總共{{count}}個商品,總價{{totalPrice}}元</h2>
<p v-if="count==0">空空如也!·······</p>
<div v-else>
<el-table :data="carData" border style="width: 100%">
<el-table-column fixed prop="id" align="center" label="商品id"></el-table-column>
<el-table-column prop="title" align="center" label="商品標(biāo)題"></el-table-column>
<el-table-column prop="price" align="center" label="商品價格"></el-table-column>
<el-table-column label="操作" align="center">
<template slot-scope="scope">
<el-button @click="reduceFun(scope.row)" type="text" size="small">-</el-button>
<span >{{scope.row.num}}</span>
<el-button @click="addCar(scope.row)" type="text" size="small">+</el-button>
<el-button @click="deleteFun(scope.row)" type="text" size="small">刪除</el-button>
</template>
</el-table-column>
</el-table>
</div>
</div>
</template>
<script>
export default {
name: "carBox",
data() {
return {};
},
computed: {
//購物車列表
carData() {
return this.$store.state.carList;
},
//商品總數(shù)
count() {
return this.$store.getters.carCount;
},
//商品總價
totalPrice() {
return this.$store.getters.totalPrice;
}
},
methods: {
// 增加數(shù)量
addCar(row){
this.$store.dispatch('addCar',row)
},
// 減數(shù)量
reduceFun(row){
this.$store.dispatch('reducedCar',row)
},
// 刪除
deleteFun(row){
this.$store.dispatch('deleteCar',row)
}
// 用戶首次登錄請求購物車的數(shù)據(jù)
// initCar(){
// this.$store.dispatch('initCar')
// }
},
created () {
// this.initCar();
},
mounted() {}
};
</script>
<style>
#carBox {
width: 900px;
margin: 0 auto;
}
</style>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue element-ui導(dǎo)航實(shí)現(xiàn)全屏/取消全屏功能
這篇文章主要介紹了vue element-ui導(dǎo)航實(shí)現(xiàn)全屏/取消全屏功能,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
vite+vue3+element-plus搭建項(xiàng)目的踩坑記錄
這篇文章主要介紹了vite+vue3+element-plus搭建項(xiàng)目的踩坑記錄,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
Vue結(jié)合ElementUI實(shí)現(xiàn)數(shù)據(jù)請求和頁面跳轉(zhuǎn)功能(最新推薦)
我們在Proflie.vue實(shí)例中,有beforeRouteEnter、beforeRouteLeave兩個函數(shù)分別是進(jìn)入路由之前和離開路由之后,我們可以在這兩個函數(shù)之內(nèi)進(jìn)行數(shù)據(jù)的請求,下面給大家分享Vue結(jié)合ElementUI實(shí)現(xiàn)數(shù)據(jù)請求和頁面跳轉(zhuǎn)功能,感興趣的朋友一起看看吧2024-05-05
VUE項(xiàng)目實(shí)現(xiàn)主題切換的多種方法
這篇文章主要介紹了VUE項(xiàng)目實(shí)現(xiàn)主題切換的方法,本文通過多種方法給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
vue.js圖片轉(zhuǎn)Base64上傳圖片并預(yù)覽的實(shí)現(xiàn)方法
這篇文章主要介紹了vue.js圖片轉(zhuǎn)Base64上傳圖片并預(yù)覽的實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
Vue電商網(wǎng)站首頁內(nèi)容吸頂功能實(shí)現(xiàn)過程
電商網(wǎng)站的首頁內(nèi)容會比較多,頁面比較長,為了能讓用戶在滾動瀏覽內(nèi)容的過程中都能夠快速的切換到其它分類。需要分類導(dǎo)航一直可見,所以需要一個吸頂導(dǎo)航的效果。目標(biāo):完成頭部組件吸頂效果的實(shí)現(xiàn)2023-04-04

