Vue.js VUEX未发现突变,但已定义

Vue.js VUEX未发现突变,但已定义,vue.js,vuejs2,vuex,Vue.js,Vuejs2,Vuex,我使用的是VUEX,在我注册了一个新的突变“shipping”之前一切都很好,因为每次我提交它时,它都告诉我:未知突变类型:shipping。但我不明白原因,因为之前的突变是正确的 这是我的代码: 突变: export function removeAllProducts (state){ state.cart = [] } export function shipping(state, cost){ state.cart.shipping = cost; }; <in

我使用的是VUEX,在我注册了一个新的突变“shipping”之前一切都很好,因为每次我提交它时,它都告诉我:未知突变类型:shipping。但我不明白原因,因为之前的突变是正确的

这是我的代码:

突变

export function removeAllProducts (state){
    state.cart = []
}

export function shipping(state, cost){
    state.cart.shipping = cost;
};
<input type="number" name="cost" :value="shippingCost" @input="updateCost">
组件

export function removeAllProducts (state){
    state.cart = []
}

export function shipping(state, cost){
    state.cart.shipping = cost;
};
<input type="number" name="cost" :value="shippingCost" @input="updateCost">
模板

export function removeAllProducts (state){
    state.cart = []
}

export function shipping(state, cost){
    state.cart.shipping = cost;
};
<input type="number" name="cost" :value="shippingCost" @input="updateCost">

你可能会说你的变异是错的。如果此行正确:

...mapMutations('cart', ['addProductToCart', 'subtractProductToCart', 'removeProductFromCart', 'removeAllProducts', 'shipping' ])
那么函数
updateCost
应该是:

  updateCost (e) {
    this.$store.commit('cart/shipping', e.target.value)
  }
如果在模块中设置为true。或者函数可以是

  updateCost (e) {
    this.shipping(e.target.value)
  }

因为您已经使用了
MapTranslations
shipping
映射到您的组件。

谢谢。有了你的建议,我不再犯错误,但州里也没有任何事情发生。在函数shipping(state,cost)中的translations.js中,我指出以下内容:state.cart.shipping=cost;但它没有任何作用:/@OscarDev在继续之前,我需要指出你的突变有一些冲突。在变异方法
removeAllProducts
中,您设置了
state.cart=[]
,但在
shipping
中,您希望更新
state.cart.shipping
,如果
cart
是一个数组,这显然没有意义。这两个突变中至少有一个是错误的,或者两者都是错误的。removeAllProducts的工作没有问题,我插入它只是为了了解我已经正确地使用VUEXTo进行了操作。要找到答案,您可以尝试将
控制台。将
日志记录到您的突变中,以查看是否调用了您的突变,更新后,国家是什么样子的。但这一部分与您的原始问题无关,因此如果您需要帮助,请创建另一个问题。我不会更新我的答案。当我想清空购物车时,请移除我使用的所有产品