[Vuejs]-Cant import vuex store to request file

0👍

You should write an action, then send your request by your action and as soon as response arrives you will be able to commit a mutation

for example in the following action:

{
  /**
   * Login action.
   *
   * @param commit
   * @param payload
   */
  login: async function ({ commit }, payload) {
    commit('LOGGING_IN')

    try {
      const result = await fetchApi({
        url: 'http://api.example.com/login',
        method: 'POST',
        body: payload
      })
      commit('LOGIN_SUCCESS', result)
    } catch (error) {
      commit('LOGIN_FAILURE', error)
    }
  }
}

as you can see above, as soon as you call login, it calls LOGGING_IN mutation and sends a request to some address, then it waits for a response.
if it gets success response the LOGIN_SUCCESS mutation with a payload of result commits otherwise it commits LOGIN_FAILURE with a payload of cached error.

note: you should provide your own fetchApi method which is a promise.

Leave a comment