Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get environment variable from Quasar Framework

Tags:

vuejs2

quasar

The environment variables are defined in config/ directory prod.env.js and dev.env.js, how to get those variable on .vue file?

I've tried using process.env.MY_VAR assuming it's nodejs built-in library, it gives an error:

[=======             ] 34% (building modules){ SyntaxError: Unexpected token (1:5)

The minimal code in .vue file:

<template>
  <q-layout>
    <div class="layout-view">
          <button class="primary" @click="foo">
            <i class="on-left">lock</i> Login
          </button>
    </div>
  </q-layout>
</template>
<script>
  export default {
    data() {
      return { url: '' }
    }
    methods: {
      foo: function() {
        this.url = process.env.MY_VAR // no error if commented
      }
    }
  }
</script>

What's the correct way to get those environment variable?

like image 266
Kokizzu Avatar asked May 02 '17 13:05

Kokizzu


People also ask

What is the use of .ENV file?

env. example file documents the application's necessary variables and can be committed to version control. This serves as a helpful reference and speeds up the onboarding process for new team members by reducing the amount of time spent digging through the coding to figure out what needs to be set up.


1 Answers

In dev.env.js and prod.env.js you write something like:

module.exports = merge(prodEnv, {
  NODE_ENV: '"development"',
  MY_VAR: '"something"'
})

Then you can use process.env.MY_VAR in your Quasar app code.

Notice the quote + double quote. The build process in Quasar uses Webpack's DefinePlugin (https://webpack.js.org/plugins/define-plugin/) which requires a JSON encoded value. Use JSON.stringify() for more complex values like JS Objects.

like image 193
Razvan Stoenescu Avatar answered Dec 16 '22 14:12

Razvan Stoenescu