Mutation does not update the store in VUEX

你说的曾经没有我的故事 提交于 2019-12-24 10:56:44

问题


Working with VUEX I am trying to update the store but I do not achieve it, I do not understand the reason since I only want to enter a numerical data without any complications.In the mutation enter messages by console and I receive them successfully but nothing happens in the state of cart.

This is my code:

Mutations.js

export function shipping(state, cost) {
    state.cart.shipping = cost;
    console.log(cost);
    console.log('hello from mutation');
}

Template:

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

Methods

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

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

Computed

computed: {
   ...mapState( 'cart', ['cart'] ),
   ...mapGetters('cart', ['totalItems', 'totalCost']),
   ...mapGetters('cart', ['shippingCost']),

  shippingCost() {
    return this.$store.getters.shippingCost;
  }
}

回答1:


Solved, we must use VUEX as an object to be able to access more elements:

Very Important

const namespaced = true;  //important!!

export default {
    namespaced,
    state,
    mutations
}

State:

export default {
    cart: {
        products: [], 
        shipping : ''
    }
}

And enter your data in this way:

computed: {
   ...mapState( 'cart', ['cart', 'products', 'shipping'] ),

Regards



来源:https://stackoverflow.com/questions/56437537/mutation-does-not-update-the-store-in-vuex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!