Search code examples
javascriptvue.jsobjectstatevuex

Vuex - How to access different state objects with one update mutation/action?


I have a bunch of objects with different strings inside those objects that I am trying to update with just one mutation and action. I am currently trying to send a string from getting an input field's value and name and using the name as a the object property to dynamically change the state, but I can't figure out how to access the correct object property dependent on the input name string.

Here's my current code,

store.js

info: {
   fullName: '',

}

....



mutations: {

   updateStateObject(state, object){
      state.info.object["name"]
   }

actions: {
   updateStateObject(store, object){
      store.commit('updateStateObject', object);
}

info.vue

updateStateObject(e){
    this.$store.dispatch(this.store+'/updateStateObject', {name: e.target.name, value: e.target.value)}

If I manually set state.info.["inputFieldName"] = object.value then it works fine, but I am trying to call the updateStateObject method from the vue file from all of my input fields and then accessing the correct property field in the state object depending on the input field name...how can I achieve this?


Solution

  • If I understand correctly, there is a name and value key on object. If so, you would do this:

    updateStateObject(state, object){
       state.info[object.name] = object.value;
    }