I'm having an issue where I am getting an unexpected value from the store.
I have a component with one method:
methods: {
setProduct(product) {
this.$store.commit('basket/empty');
console.log(this.$store.state.basket.items);
this.$store.commit("basket/addToBasket", {
id: product.id
});
},
},
It simply empties the basket and adds a new product, thus one product can exist in the basket at any one time.
My issue is that the console log is returning a product.
Rather than an empty array. If I remove
this.$store.commit("basket/addToBasket", {
id: product.id
});
it is returns
Please can someone tell me why the console.log is returning items, when the commit hasn't taken place yet?
Basket.js
const state = {
items: []
};
const getters = {};
const actions = {};
const mutations = {
empty (state) {
state.items = [];
},
addToBasket (state, product) {
state.items.push(product);
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations
};

