I'm trying to add or update an object in store with Vuex.
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
state: {
userData: {}
},
mutations: {
ADD_USER_DATA: (state, data) => {
state.userData.push(data)
}
}
})
This returns state.userData.push
is not a function.
And in the components:
<template>
<div>
<input type="date" v-model="inputData.date1">
<input type="date" v-model="inputData.date2">
<input type="number" v-model="inputData.date3">
<button @click="submitForm">Submit</button>
</div>
</template>
<script>
import { mapState, mapMutations } from 'vuex'
export default {
data () {
return {
inputData: {}
}
},
computed: {
...mapState([
'userData'
])
},
methods: {
...mapMutations([
'ADD_USER_DATA'
]),
submitForm () {
this.ADD_USER_DATA(this.inputData)
}
}
}
</script>
Later on I want to update userData
with a value from other component, so that it impacts both components. I would like to have a nice way of adding, replacing, concating the old array with a new array. I followed the example in this video.
(FYI: I'm currently learning Vue.js from scratch and couldn't figure out this Vuex's mutations, actions...)
userData
should be an array and it would be declared as :state: { userData: [] },
– GarretgarrethuserData
but with an object you can't. If I changeuserData.push
toVue.set
then it works but later on I can't add or replace an value. – UndeviatinguserData
contain the data of one user? – Garretgarrethstate.userData=data
– Garretgarreth