I have a simple cart store, to add / remove items.
When you add a product to the cart, I show a flash message with the possibility to update the quantity. Since I don't know which product is concerned in my flash message component, I use the store.
I have a simple bootstrap spinner :
<b-form-spinbutton
:value="value"
min="1"
max="26"
@change="setItems"
/>
When I change this, I want to call the setItems
function of my store to update the last item added.
So I used :
data () {
return {
value: 1
}
},
methods: {
setItems (value) {
const items = [...this.$store.getters['cart/items']]
// update quantity of the last item - we suppose it's the current item
items.slice(-1)[0].quantity = value
this.$store.commit('cart/setItems', items)
}
}
I read about this error here : Vuex - Do not mutate vuex store state outside mutation handlers, so I changed v-model
to value
approach.
My store cart.js
is :
export const state = () => ({
items: []
})
export const mutations = {
setItems (state, items) {
state.items = items
},
// ....
I can't figure how to handle this problem ?