How to initialize the way that `state` uses the network request API?

how do I initialize state how to use the network to request API?

const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: "...", done: true },
      { id: 2, text: "...", done: false }
    ]
  },
  

I have a todos in state, I want to initialize it by requesting API. How do I write it here?

Mar.12,2021

import   {gettodos} from 'api'
state: {
    todos: []
  },
  mutations: {
    SET_TODOS: (state,todos) => {
      state.todos = todos
    }
  },
  actions: {
    getTodos({ commit }) {
      return new Promise(resolve => {
        gettodos(state.token).then(res => {
          commit('SET_TODOS', res.data.xxxx)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }

in the place where you initialize this.$store.dispatch (getTodos, {parameter})
if the data is returned to the page, you directly want to add resolve (res.data)

.
Menu