Middle
Have you worked with Vuex? What are mutations in Vuex?
sobes.tech AI
Answer from AI
Vuex is a centralized store for the state of Vue.js applications. Mutations in Vuex are the only way to change the store's state. They are synchronous and are described as functions that accept the current state and a payload, then modify the state.
Example of a mutation:
const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment(state, payload) {
state.count += payload.amount;
}
}
});
store.commit('increment', { amount: 5 });
console.log(store.state.count); // 5
Using mutations ensures predictability and traceability of state changes, which is important for debugging and state management.