VUEX找不到突变,但已定义

时间:2019-06-04 02:57:00

标签: vue.js vuejs2 vuex

我正在与 VUEX 合作,一切都很好,直到我注册了一个新的突变“发货”,因为每次我提交它都会告诉我:未知突变类型:运输 。但是我不明白原因,因为先前的突变可以正常工作。

这是我的代码:

变异

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

export function shipping(state, cost){
    state.cart.shipping = cost;
};

组件

模板

<input type="number" name="cost" :value="shippingCost" @input="updateCost">

脚本:

computed: {
   ...mapState( 'cart', ['cart'] ),
   ...mapGetters('cart', ['totalItems', 'totalCost', 'shippingCost']),
    shippingCost:{
      get(){ return this.$store.getters.shippingCost; },
    } 
},



     methods: {
     ...mapMutations('cart', ['addProductToCart', 'subtractProductToCart', 'removeProductFromCart', 'removeAllProducts', 'shipping' ]),
     ...mapMutations('routes', ['addRoute']),

      updateCost (e) {
        this.$store.commit('shipping', e.target.value)
      }
   }
}

1 个答案:

答案 0 :(得分:2)

您可能会说您的突变是错误的。如果此行正确:

...mapMutations('cart', ['addProductToCart', 'subtractProductToCart', 'removeProductFromCart', 'removeAllProducts', 'shipping' ])

然后函数updateCost应该是:

  updateCost (e) {
    this.$store.commit('cart/shipping', e.target.value)
  }

如果您在模块中确实将namespacing设置为true。或者功能可以是

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

因为您已经使用mapMutations来将shipping映射到组件。