Whereas you are using the cli.vuejs.org to generate your project and build,
I suggest using environment variable files, .env
Mimics the use of Node environment variables.
Then you create your files .env
for its environments (development, homologation and production) with the variables:
NODE_ENV=development
VUA_APP_API_BASE_URL=http://localhost:8080/api
VUE_APP_ is the mandatory prefix for variables to be injected into your app
And then in your code just use process.env.VUE_APP_API_BASE_URL
I no longer know if it is possible to use the process.env.VUE_APP_X
in expressions in the <template>
, but I suggest using computed properties in its components or even creating a component just to abstract it.
<template>
<img :src="src" />
</template>
<script>
export default {
name: 'Image',
props: ['path'],
computed: {
src({ path }) {
return `${process.env.VUE_APP_API_BASE_URL}/${path}`;
}
}
}
</script>
And use the component:
<Image :path="`/uploader/hotels/${hotel.photo}`"/>
And in the place where you set the base url of Axios, replace by process.env.VUE_APP_API_BASE_URL
.
I tested the first option and it does not work in template as it was said. I tested the second and the page gets stuck. The main reason I want to use it this way is because in Axios, the url of the server was already defined and just concatenate with the corresponding location. If the system changes server, I would change in one place. I think creating a component just for that would be too bureaucratic. It will be if I can set a global string type variable at a location in the quasar framework just to access the server address anywhere in the system?
– Temístocles Arêa
Relax, I think now I understand... Did you want to use only the url already set in Next as default.baseUrl to concatenate the url you put in src? Or you want to call a service that a url and finally use the returned url of the service as src of the image?
– Giovane
Only the url solves the problem. I was searching if there was a place to put these variables so I could also use in Xios.
– Temístocles Arêa
Okay, so you just need to use the url... quiet, I’ll edit my answer
– Giovane
The proposed solution is interesting! Because it used as a base . env that can be changed easily for each environment! However, I found it strange to have to redo the img tag as Component. It would not be more interesting for the
process.env.VUE_APP_API_BASE_URL
direct on Xios?– Temístocles Arêa
Yes, I mentioned the Image component as an example. It’s just that I personally don’t find it interesting to keep using the
$axios.defaults.baseURL
... I would already set out for the strategy of a specific component to abstract this work of always putting theprocess.env.VUE_APP_API_BASE_URL
or create a type functionresolveImgSrc
and always use it to mount the path.– Giovane