Package Exports
- @nuxtjs/axios
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@nuxtjs/axios) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Axios
This plugin is a wrapper around axios.
- Sets default base URL.
- Handles all HTTP exceptions and prevents server side unhandled promise exceptions.
- Injects
$get,$post,... into vue context instances so requests can be done easily. - Exposes
setTokenfunction to$axiosso we can easily and globally set authentication tokens. - Throws nuxt-friendly exceptions.
- Automatically enables
withCredentialswhen requesting to default base URL.
Setup
- Add
@nuxtjs/axiosdependency using yarn or npm to your project - Add
@nuxtjs/axiosmodule tonuxt.config.js:
modules: [
'@nuxtjs/axios'
]Usage
Inside asyncData
async asyncData({app: {$axios}}) {
const {data} = await $axios.get('http://icanhazip.com')
return {
ip: data
}
}Inside component methods
async mounted() {
const {data} = await this.$get('http://icanhazip.com')
this.ip = data
}Customization
Customization can be done using shared environment variables.
| Environment variable | Default | Description |
|---|---|---|
| API_URL | http://localhost:3000 | Base url for ajax requests in server-side |
| API_URL_BROWSER | [API_URL] | Base url for ajax requests in client-side |
| API_PREFIX | /api | Adds this prefix before all relative urls |