写了一个购物车,添加商品时多次点击同一个商品只显示数量1,添加第二件商品时第一件商品的数量就变成正确的了,数据变化时没有监听到变化,请问各位大佬该怎么解决?
store.js:
isLogin:false,
car:[]
},
getters:{
clearUp:function(state){
return state.car.sort(function(m,n){n-m})
}
},
mutations:{
addCar:function(state,g){
var swi = -1
for(var i in state.car){
if(state.car[i].id == g.id){
swi = i
break
}
}
//console.log(swi)
if (swi != -1){
state.car[swi].num ++
}else{
g.num = 1
state.car.push(g)
}
//console.log(state.car)
}
}
监听页:
chuan:function(){
return this.$store.getters.clearUp
}
},
watch:{
clearUp:function(){
console.log(this.$store.state.car)
return this.$store.state.car
}
}
结构:
```