如何将 API 响应中的数据分配给状态?

How to assign data from API response to state?

我正在从 API 中检索数据。但是我不能把那个对象赋值给vuex中的state对象。

这是我的状态对象

 state: {
    calendarOptions:[],
}

如何通过 api 调用将数据分配给 calendarOptions?

在我的行动中

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

this.state.calendarOptions = response.data

}

我需要这样的东西?

vuex 的文档中解释了如何做到这一点
要改变状态,你必须使用 mutations

示例

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

           commit("setCalendarOptions", response.data)
       }
    }
}