How to access axios inside store action?
-
I have the standard boot file for axios
import Vue from 'vue' import axios from 'axios' Vue.prototype.$axios = axios
How can I access axios inside a vuex store action?
-
Just import axios in your
action.js
file -
@realtebo take the example from the docs https://quasar.dev/quasar-cli/cli-documentation/boot-files#Accessing-data-from-boot-files.
-
Here is a clip from my Vuex store.
const actions = { async signIn({ dispatch, commit }, credentials) { console.log('login action' + JSON.stringify(credentials)) var authenticateResponse = await this._vm.$axios.post("user/authenticate", credentials) // console.log("My user obj: " + JSON.stringify(authenticateResponse.data)) // do something with response }, ... }
-
@metalsadman said in How to access axios inside store action?:
@realtebo take the example from the docs https://quasar.dev/quasar-cli/cli-documentation/boot-files#Accessing-data-from-boot-files.
Also for anyone interested, the now broken link above has been moved here: https://quasar.dev/quasar-cli/boot-files#Accessing-data-from-boot-files
-
You can simply assign your axios instance in a boot file.
Insidesrc/boot/axios.js
you can use this:import axios from 'axios' export default ({ store, Vue }) => { Vue.prototype.$axios = axios store.$axios = axios }
Once it’s set up like this, axios can be accessed in your store actions as
this.$axios
.