Answer the question
In order to leave comments, you need to log in
How to correctly add an object to the store?
There is a form, 3 text fields. The task is to add information from them to the object, and add this object to the array that lies in the store.
How do I do add:
submit on the form. The object is assembled into a vehicle object from the v-model fields.
<form @submit.prevent="submit">
<input
type="text"
placeholder="Name"
v-model="vehicle.name"
/>
<input
type="text"
placeholder="Description"
v-model="vehicle.description"
/>
<input
id="rentInput"
type="text"
placeholder="Rent price"
v-model="vehicle.rent"
/>
</div>
<button type="submit" :class="$style.modalSubmit">Complete</button>
</form>
data: () => ({
vehicle: {
name: '',
description: '',
rent: '',
type: 'custom'
}
}),
methods: {
submit() {
this.$store.commit("vehicles/addVehicle", this.vehicle);
},
}
export const state = () => ({
vehicles: [],
});
export const mutations = {
addVehicle(state, payload) {
let vehicles = state.vehicles.concat(payload);
state.vehicles = vehicles;
},
};
Error: [vuex] do not mutate vuex store state outside mutation handlers.
Answer the question
In order to leave comments, you need to log in
Error: [vuex] do not mutate vuex store state outside mutation handlers.
this.$store.commit('vehicles/addVehicle', { ...this.vehicle });
// или
addVehicle: (state, payload) => state.vehicles.push({ ...payload }),
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question