Почему при добавлении товара в корзину мне выдает ошибку
[Vue warn]: Error in v-on handler: "TypeError: null is not an object (evaluating 'state.cart[item.id]')"
?
import Vue from "vue";
import Vuex from "vuex";
let cart = window.localStorage.getItem('cart');
let cartCount = window.localStorage.getItem('cartCount');
Vue.use(Vuex);
export const store = new Vuex.Store({
state: {
cart: cart ? JSON.parse(cart) : {},
cartCount: cartCount ? parseInt(cartCount): 0,
},
mutations: {
addToCart(state, item) {
let cartItem = state.cart[item.id];
if(state.cart[item.id]){
cartItem.quantity++
cartItem.totalPrice = cartItem.quantity * cartItem.price;
}
else{
state.cart[item.id] = item;
Vue.set(item, 'quantity', 1);
Vue.set(item, 'totalPrice', item.price);
}
state.cartCount++;
this.commit('saveCart');
},
saveCart(state){
window.localStorage.setItem('cart', JSON.stringify(state.cart));
window.localStorage.setItem('cartCount', state.cartCount);
},
},
})