Answer the question
In order to leave comments, you need to log in
How to bypass this magic?
Wrote this code in vuex mutation:
ADD_TO_CART: function(state, payload) {
let found = state.cartItems.find(i => i.id == payload.id);
if (found){
found.count = count + payload.count;
}
else {
state.cartItems.push(payload);
}
window.localStorage.setItem('cart', JSON.stringify(state.cartItems));
}
//state.cartItems[index].count = 1, payload.count = 5 (под index подразумевается индекс объекта, который будем менять)
ADD_TO_CART: (state, payload) =>{
//уже тут state.cartItems[index].count = 5, payload.count = 5 (а вот почему - хз)
let index = state.cartItems.findIndex(i => i.id == payload.id);
if (index > -1){
state.cartItems[index].count += payload.count;
//state.cartItems[index].count = 10, payload.count = 5
}
else {
state.cartItems.push(payload);
}
window.localStorage.setItem('cart', JSON.stringify(state.cartItems));
}
Answer the question
In order to leave comments, you need to log in
I don't quite understand why you don't get the error count is not defined
apples = 5;
basket = 0;
basket = apples; // тупо присваем в корзину яблоки
basket += apples; // прибавляем яблок к тем, что уже есть корзине
basket = basket + apples; // тоже самое что и выше
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question