Search code examples
vue.jsvuex

How to assign data from API response to state?


I am retrieving the data from API. But I can't assign that object to the state object in vuex.

HERE IS MY STATE OBJECT

 state: {
    calendarOptions:[],
}

how will I assign data to calendarOptions by api call?

And in My Action

actions: {
//some api call, and its response 

this.state.calendarOptions = response.data

}

i need something like this ?


Solution

  • in docs of vuex explained how to do that
    for changing state you have to use mutations

    Example

    export default {
        state: {
            calendarOptions:[],
        },
        mutations: {
            setCalendarOptions(state, data) {
                state.calendarOptions = data
            }
        },
        actions: {
           getCalendarOption({commit}) {
               //some api call, and its response 
    
               commit("setCalendarOptions", response.data)
           }
        }
    }