Use global vars with webpack and Vue.js

Webpack

Open config/dev.env.js and in the merge function below NODE_ENV (…), add the following globalĀ var for the development environment:
MY_VAR: JSON.stringify('dev_value')

Open config/prod.env.js and in the merge function, add the following global var for the production environment:
MY_VAR: JSON.stringify('prod_value')

You’ll then have access to your varĀ in your code.

Vue.js Code

If you want to use a global var, in your src/main.js add:
Vue.prototype.$VARNAME = process.env.MY_VAR

Anywhere in your code, you can then use:
this.$VARNAME

Leave a Reply

Your email address will not be published. Required fields are marked *