本文实例为大家分享了vuex实现购物车增加减少移除的具体代码,供大家参考,具体内容如下
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;
// 判断如果购物车是否有这个商品,有就只增加数量,否则就添加一个
// 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 {
money += item.price * item.num
})
return money;
} else {
return 0;
}
},
//返回购物车的总数
carCount(state) {
return state.carList.length
}
},
})
2. list.vue(商品列表)
跳转到购物车 加入购物车 #listBox { width: 900px; margin: 0 auto; }
3. car.vue(购物车)
合计:总共{{count}}个商品,总价{{totalPrice}}元
空空如也!·······
- {{scope.row.num}}+ 删除 #carBox { width: 900px; margin: 0 auto; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



