vue-plugin-axios
A wrapper for integrating axios to Vuejs
3,521
5
| Install | |
|---|---|
npm install vue-plugin-axios |
|
| Latest Version: | 1.0.14 |
| Vue: | 1 · 2 |
| License: | MIT |
| Last Updated: | Feb 28, 2024 |
| Links: | GitHub · npm |
Maintainer: ilyazelenko
Usage
Add Vue plugin
import Vue from 'vue'
import VueAxios from 'vue-plugin-axios'
import axios from 'axios'
import store from '@/store'
Vue.use(VueAxios, {
axios,
// example config for axios instance
config: { // axios instance config
baseURL: 'http://localhost:8000/', // api URL
headers: { ... }, // default headers
...
},
interceptors: {
// this function shows how to add Authorization header to requests
beforeRequest (config, axiosInstance) {
// your auth token
const token = store.state.auth.token
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
},
// this function shows how to add errors from server to client app
beforeResponseError (error) {
const { response, message } = error
if (response) { // backend error
// shows response error
alert(error.response.data.message)
} else if (message) { // network error
alert(message)
}
// return Promise.reject(error)
}
}
})
Now you can access from a component:
// no need to import
export default {
data: () => ({
users: null
}),
async mounted () {
// automatic data return, no need to ".data"
this.users = await this.$get('api/get-users/')
const randomUsers = await this.$get('https://randomuser.me/api/')
}
}
Installation
npm install vue-plugin-axios --saveoryarn add vue-plugin-axios- add
Vue.uselike in code above
This is how you can use with Nuxt.js (client + server):
export default ({ store, app }, inject) => {
// Vue.use(VueAxios, for some reason did not work
VueAxios.install(Vue, {
axios,
nuxtInject: inject
})
}
Full API
$get(endpoint[, config])
$delete(endpoint[, config])
$head(endpoint[, config])
$options(endpoint[, config])
$post(endpoint[, data[, config]])
$put(endpoint[, data[, config]])
$patch(endpoint[, data[, config]])
Axios instance:
$axios or $http
For full response you can use:
const { data, status, statusText, headers } = await this.$axios.post('/api/get-users')
Set custom header:
$axios.setHeader('X-Custom-Header', 'value')
or multiple:
$axios.setHeader({
auth: token, // thiw will set 'Authorization' header, no need prefix 'Bearer '
'X-Custom-Header': 'value',
'Y-Custom-Header': 'value'
})
Set Authorization token header:
$axios.setToken(token[, type = 'Bearer'])
Delete header:
deleteHeader('X-Custom-Header')
deleteHeader(['X-Custom-Header', 'Y-Custom-Header'])
Related Packages
v-api-fetch
Um plugin e composable para Vue 3 que simplifica chamadas de API com Axios, incl...
2,404
3
@mundoit-lib/plugin-vue-axios
Vue 3 plugin for a shared Axios instance (JSON headers, multi-database base URL,...
1,637
0
Version History
| Version | Released | Vue | License |
|---|---|---|---|
| 1.0.14 | >= 1.0.0 | MIT | |
| 1.0.13 | >= 1.0.0 | MIT | |
| 1.0.12 | >= 1.0.0 | MIT | |
| 1.0.11 | >= 1.0.0 | MIT | |
| 1.0.10 | >= 1.0.0 | MIT | |
| 1.0.9 | >= 1.0.0 | MIT | |
| 1.0.8 | >= 1.0.0 | MIT | |
| 1.0.7 | >= 1.0.0 | MIT | |
| 1.0.6 | >= 1.0.0 | MIT | |
| 1.0.5 | >= 1.0.0 | MIT | |
| 1.0.4 | >= 1.0.0 | MIT | |
| 1.0.3 | >= 1.0.0 | MIT | |
| 1.0.2 | >= 1.0.0 | MIT | |
| 1.0.0 | >= 1.0.0 | MIT |