具有自定义类JavaScript的Vuex无法提交突变

时间:2019-03-12 14:35:23

标签: javascript vue.js

这是我关于vue和vuex的第一个项目。 该项目是一个小的销售人员应用程序。

当我介绍我的javascript类以放置在商店中时出现问题

当我尝试删除或更新商店时,我收到未定义的信息

我的动作是这样的

export const addProduct = ({ state, commit }, product, variant) => {
  const exists = state.cart.find(i => {
    i.RoaCodart === product.ranCodart;
  });

  if (exists) {
    commit("updateQuantity", product, variant);
  } else {
    commit("addProduct", product, variant);
  }
};

export const removeProduct = ({state, commit}, product)=>{
    console.log('removeProduct ' + product)
    const index = state.cart.findIndex(i => i.RoaCodart === product.RoaCodart);
    commit("removeProduct", index);
};

这是我的变异:

import RigheOrdine from "../store/modules/RigheOrdine.js";

export const addProduct = (state, product, variant) => {
  console.log(variant)
  const row = new RigheOrdine("001", product.ranCodart, variant, product.ranDescri, product.ranPrezz1, "00001", new Date().getDate(), "001", "", "00001");
  row.RoaQuanti = 1; 
  state.cart.push(row);

};

export const updateQuantity = (state, product, variant) => {
  const index = state.cart.indexOf(product);
  let orderItem = state.cart[index];
  if (orderItem !== undefined) orderItem.RoaQuanti++;

  state.cart.splice(index, 1, orderItem); //Object.assign({}, orderItem) ??
};

export const removeProduct = (state, index)=>{
  state.cart.splice(index, 1);
};

因此,每当我尝试在商店中检测到产品时,我都会收到未定义的提示!

import Variants from "./Variants.vue";
export default {
  name: "product-details",
  props: {
    product: {
      type: Object,
      required: true
    }
  },
  components: {
    Variants
  },
  methods: {
    back() {
      this.$router.go(-1);
    },
    add(varianti) {
      if(!varianti.length)
       // this.$store.dispatch("addProduct", this.product, '');
        this.$store.dispatch('addProduct', { product, variant });
      else
        varianti.forEach(variant => {
           this.$store.dispatch("addProduct", this.product, variant);
      });
    },
    qty(codart){
       return this.$store.getters.quantity(codart);
     }
  }
};
</script>

1 个答案:

答案 0 :(得分:0)

如果需要多个有效负载,则需要将Object作为第二个参数发送。 See here

  

在大多数情况下,有效负载应该是一个对象,以便它可以包含多个字段

Case