代码粘出来哈
let footerCart={
template:"#footerCart",
computed:{
totalPrice(){
return this.$store.getters.totalPrice
}
}
}
let Lists = {
template: '#Lists',
computed: {
/* totalPrice() {
return this.$store.getters.totalPrice
},*/
goods() {
return this.$store.getters.goods;
}
},
methods: {
del(id) {
this.$store.commit('del', { id })
}
}
}
let store = new Vuex.Store({
state: {
goods:[]
},
getters: {
//获取商品总价
totalPrice: state => {
let totalPrice = 0;
state.goods.forEach((v) => {
totalPrice += v.num * v.price;
})
return totalPrice;
},
/*totalPrice:function(state){}*/
//获取商品并计算每件商品的总价
goods(state) {
let goods = state.goods;
goods.forEach((v) => {
v.totalPrice = v.num * v.price;
});
return goods;
}
},
mutations: {
del(state, param) {
console.log(param);
let k;
for(var i=0;i<state.goods.length;i++)
{
if (state.goods[i].id == param.id) {
k = i;
break;
}
}
state.goods.splice(k, 1);
},
setGoods(state,param){
state.goods=param.goods;
}
},
actions:{//actions用来处理异步的
loadGoods(store){
axios.get('73.php').then(function(response){
store.commit('setGoods',{goods:response.data})
console.log(response);
});
}
}
});
var app = new Vue({
el: '#app',
store,
components: {
Lists,
footerCart
},
mounted(){
this.$store.dispatch('loadGoods');
}
})
DontForgetMe520
相关分类