如何用vuex实现购物车的增加减少移除-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!

这篇文章主要介绍如何用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
}
},
}) 分享文章:如何用vuex实现购物车的增加减少移除-创新互联
文章链接:http://www.scyingshan.cn/article/dpdsjg.html


咨询
建站咨询
