Using Environment Variables with Vue.js

后端 未结 9 1331
礼貌的吻别
礼貌的吻别 2020-11-29 18:22

I\'ve been reading the official docs and I\'m unable to find anything on environment variables. Apparently there are some community projects that support environment variabl

相关标签:
9条回答
  • 2020-11-29 18:27
    1. Create two files in root folder (near by package.json) .env and .env.production
    2. Add variables to theese files with prefix VUE_APP_ eg: VUE_APP_WHATEVERYOUWANT
    3. serve uses .env and build uses .env.production
    4. In your components (vue or js), use process.env.VUE_APP_WHATEVERYOUWANT to call value
    5. Don't forget to restart serve if it is currently running
    6. Clear browser cache

    Be sure you are using vue-cli version 3 or above

    For more information: https://cli.vuejs.org/guide/mode-and-env.html

    0 讨论(0)
  • 2020-11-29 18:33

    For those using Vue CLI 3 and the webpack-simple install, Aaron's answer did work for me however I wasn't keen on adding my environment variables to my webpack.config.js as I wanted to commit it to GitHub. Instead I installed the dotenv-webpack plugin and this appears to load environment variables fine from a .env file at the root of the project without the need to prepend VUE_APP_ to the environment variables.

    0 讨论(0)
  • 2020-11-29 18:35

    In the root of your project create your environment files:

    • .env
    • .env.someEnvironment1
    • .env.SomeEnvironment2

    To then load those configs, you would specify the environment via mode i.e.

    npm run serve --mode development //default mode
    npm run serve --mode someEnvironment1
    
    

    In your env files you simply declare the config as key-value pairs, but if you're using vue 3, you must prefix with VUE_APP_:

    In your .env:

    VUE_APP_TITLE=This will get overwritten if more specific available
    

    .env.someEnvironment1:

    VUE_APP_TITLE=My App (someEnvironment1)
    

    You can then use this in any of your components via:

    myComponent.vue:

    <template>
      <div> 
        {{title}}
      </div>
    </template>
    
    <script>
    export default {
      name: "MyComponent",
      data() {
        return {
          title: process.env.VUE_APP_TITLE
        };
      }
    };
    </script>
    

    Now if you ran the app without a mode it will show the 'This will get...' but if you specify a someEnvironment1 as your mode then you will get the title from there.

    You can create configs that are 'hidden' from git by appending .local to your file: .env.someEnvironment1.local - very useful for when you have secrets.

    Read the docs for more info.

    0 讨论(0)
  • 2020-11-29 18:46

    If you are using Vue cli 3, only variables that start with VUE_APP_ will be loaded.

    In the root create a .env file with:

    VUE_APP_ENV_VARIABLE=value
    

    And, if it's running, you need to restart serve so that the new env vars can be loaded.

    With this, you will be able to use process.env.VUE_APP_ENV_VARIABLE in your project (.js and .vue files).

    Update

    According to @ali6p, with Vue Cli 3, isn't necessary to install dotenv dependency.

    0 讨论(0)
  • 2020-11-29 18:46

    This is how I edited my vue.config.js so that I could expose NODE_ENV to the frontend (I'm using Vue-CLI):

    vue.config.js

    const webpack = require('webpack');
    
    // options: https://github.com/vuejs/vue-cli/blob/dev/docs/config.md
    module.exports = {
        // default baseUrl of '/' won't resolve properly when app js is being served from non-root location
        baseUrl: './',
        outputDir: 'dist',
        configureWebpack: {
            plugins: [
                new webpack.DefinePlugin({
                    // allow access to process.env from within the vue app
                    'process.env': {
                        NODE_ENV: JSON.stringify(process.env.NODE_ENV)
                    }
                })
            ]
        }
    };
    
    0 讨论(0)
  • 2020-11-29 18:47

    In vue-cli version 3:

    There are there options for .env files: Either you can use .env or:

    • .env.test
    • .env.development
    • .env.production

    You can use custom .env variables by using the prefix regex as /^/ instead of /^VUE_APP_/ in /node_modules/@vue/cli-service/lib/util/resolveClientEnv.js:prefixRE

    This is certainly not recommended for the sake of developing an open source app in different modes like test, development, and production of .env files. Because everytime you npm install .. , it will be overrided.

    0 讨论(0)
提交回复
热议问题