2

I am using the pattern to store config variables in my /config/[xxx].env.js files like so:

module.exports = merge(prodEnv, {
  NODE_ENV: '"development"',
  API: {
    ROOT: "'http://blah.blah.blah'"
  },
  CONNECTORS: {
    SPOTIFY: {
      CLIENT_ID: "'blah'",
      SCOPES: "'blah'",
      REDIRECT_URI: "'blah'"
    }
  }
})

Note that some of these aren't specifically environment variables, but I want to put these type of config vars in a centralized location.

In my main.js, I can access the variables fine via process.env:

Vue.http.options.root = process.env.API.ROOT

But in a lower level component, "process" does not exist.

I'm aware that it shouldn't, since its not globally defined, but I cannot figure out how to get to that process.env object from within a component. I know I can make it global, but that doesn't "feel right."

Two questions:

  1. Is there some other Vue import / object that I'm missing to get to process.env?

  2. Is there a better way to store these type of centralized values, that might be environment dependent? I've seen things like the ruby config gem that does a nice job of layering environment specific configs on top of default configs.

I did try the npm config gem, but for some reason it is missing other required npms, and appears to be geared toward node.js, AND it feels like I'm doing something wrong not using what vue-cli webpack provides.

1 Answer 1

3

In general the accepted 'industry standard' way to have components be able to see data from other components, or form really anywhere is to use VueX. It's the analogous thing to Redux in React. It seems a bit complex at first but in reality it's simple, and VueX relieves you of all the hasles of how any given component can see any given data, and solves lots of other issues at the same time. try it out.

Of course another way is to have a module in your app that implements a kind of 'singleton pattern' so that any code can just dump data in and get data out.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, I use Vuex already. That makes sense, I'll just dump it into the store on the app load.
Vuex is meant for state that will change after the application has loaded for the client. If these build config variables aren't going to change through the vue application's lifecycle you may be better off putting them inside a service container, like vue-container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.