【发布时间】:2021-09-03 22:38:43
【问题描述】:
我有一个简单的购物车商店,用于添加/删除商品。
当您将产品添加到购物车时,我会显示一条可以更新数量的消息。由于我不知道我的闪信组件中涉及到哪个产品,所以我使用商店。
我有一个简单的引导微调器:
<b-form-spinbutton
:value="value"
min="1"
max="26"
@change="setItems"
/>
当我改变这个时,我想调用我商店的setItems 函数来更新最后添加的项目。
所以我用了:
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)
}
}
我在这里读到了这个错误:Vuex - Do not mutate vuex store state outside mutation handlers,所以我将v-model 更改为value 方法。
我的店铺cart.js 是:
export const state = () => ({
items: []
})
export const mutations = {
setItems (state, items) {
state.items = items
},
// ....
我不知道如何处理这个问题?
【问题讨论】: