axios-module: baseURL settings do not take effect for axios
Version
Reproduction link
https://github.com/begueradj/nuxt-axios-baseurl-bug
Steps to reproduce
- Clone my repo and change to the resulted directory
- Intall the dependencites:
yarn install - Launch the server:
yarn run dev
What is expected ?
expect the number of displayed posts to be 100.
This means in pages/index.vue, when I have this code:
mounted() {
axios.get('/posts')
.then(response => {
this.posts = response.data
})
.catch(e => {
this.errors.push(e)
})
}
axios should recoginize the baseURL configuration key I set in nuxt.config.js and thus fetch data from http://jsonplaceholder.typicode.com/posts
Here is my nuxt.config.js file content related to axios module:
modules: [
// Doc: https://github.com/nuxt-community/axios-module#usage
['@nuxtjs/axios', {
baseURL: 'http://jsonplaceholder.typicode.com'
}]
],
/*
** Axios module configuration
*/
axios: {
// See https://github.com/nuxt-community/axios-module#options
},
What is actually happening?
-
The number of displayed posts is 0
-
axios is trying to send a get request to
http://localhost/postsinstead of http://jsonplaceholder.typicode.com/posts
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 23 (3 by maintainers)
it worked for me adding
require('dotenv').config()to the nuxt.config.js@skewness This worked for me as well…
I think the docs should be clear about this. They just say “They can be customized with API_PREFIX, API_HOST (or HOST) and API_PORT (or PORT) environment variables.”
which is not true, unless the
.envfile is loaded… unless it means passing in the env variables as arguments? ¯_(ツ)_/¯It does not work in my case even when I used
$axiosIn .vue components, if I called
process.env.apiUrlit will return “http://localhost:8000” which is correct as expected. But if I callthis.$axios.defaults.baseURLit will be empty.Why?
Its not a bug. You are import fresh instance of axios instead of using this.$axios It clearly described in docs how you should use it https://axios.nuxtjs.org/usage
If only it was this easy…
Just remember to do
npm run devagain after change settings -_-@pawel-marciniak yes, if you want to use
dotenvuse it like that@sangdth
Hi! I was able When you are doing
This is what worked for me @marko-mlinarevic and I believe it should work for you too:
no, there wont be difference in terms of performance