I am trying to expose env variables to my other js files. So i have other JS files that i structured it so that api's and model lives in its own file and folder and when i want to use them i will import it using
import Auth from '@services/api/auth'
And in that file i would like to expose env variables like how i can do it in vue components script file like
<script>
import login from '@/components/view/login/login'
export default {
components: {
login
},
created () {
console.log(process.env.API_BASE_URL)
}
}
</script>
So i am using standard Vue 2.5 with webpack 3.1 and currently i can call process.env.API_BASE_UR
L inside .vue
file but if i try to call that in imported .js
file i get undefine.
Like this
import Axios from 'axios'
export default {
login (username, password) {
return new Promise((resolve, reject) => {
console.log(process.ENV.API_BASE_URL)
let url = process.ENV.API_BASE_URL + 'oauth2/token'
let body = {
username: username,
password: password,
scope: process.ENV.API_SCOPE.LOGIN.scope,
grant_type: process.ENV.API_SCOPE.LOGIN.grant_type
}
let header = {
auth: {
username: process.ENV.API_CLIENT.LOGIN.username,
password: process.ENV.API_CLIENT.LOGIN.password
}
}
return Axios.post(url, body, header)
.then(res => {
resolve(res)
})
.catch(err => {
reject(err)
})
})
}
}