问题
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