使用vuex怎么购物车里勾选了的商品放进下单页面
购物车的code
删除
总计:¥{{sum}}
继续购物
//下单页面按钮
下单
toggleSelect(id){
var i=this.findPosition(id);
var select=this.goodsList[i].select;
this.updatedGoods({
index:i,
key:"select",
value:!select //这里切换false和true
})
}
vuex code
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state:{
goodsList:localStorage["goodsList"]?JSON.parse(localStorage["goodsList"]):[]
},
getters:{
sum:state=>{
var total=0;
state.goodsList.forEach((item)=>{
if(item.select){
total=(Number(total)+Number(item.price*item.number)+Number(item.freight)).toFixed(2)
}
})
return total
},
gooddsNumber:state=>{
return state.goodsList.length
}
},
mutations:{
getData(state,val){
state.goodsList.push(val);
localStorage.setItem("goodsList",JSON.stringify(state.goodsList))
},
deleteGoods(state,index){
state.goodsList.splice(index,1);
localStorage.setItem("goodsList",JSON.stringify(state.goodsList))
},
updatedGoods(state,data){
const {index,key,value}=data;
if(state.goodsList[index].inventory>=value){
state.goodsList[index][key]=value;
}else{
alert("库存不足")
}
localStorage.setItem("goodsList",JSON.stringify(state.goodsList))
}
},
actions:{
},
})
这个是之前放在getter中 把它存进本地存储
显示为空