0

I ran into an issue where I would need to get the app url from env in my vue template. And I found out that to get something from env in vue, they need a prefix "VUE_APP". Eventually resolved it by using: window.location.origin+'/'. Thing is, I wont always have control over the naming of env or the customer (or if they insist on NOT putting anything on constant.php). And someday I might require something that cant be replicated by anything else. Is there a way to get env variables without the prefix?

Note: I know this has been asked before here: Vue cli env without prefix VUE_APP

But all this says is: dont do this. So what can I do if I face something like this? Is there a workaround or do I just pray that I dont get screwed by the requirements?

3
  • You'd need to modify the way Vue CLI works and the parts of it that are responsible for the handling of env vars. Vue CLI is just a low-maintenance Webpack setup. You can create your own and do anything you want. You shouldn't expose all env vars because it may be wasteful and unsafe to expose all of them to the app that run in a browser. They could contain private keys, etc. This is the reason why the ones that are safe to use are explicitly prefixed. Consider reasking and explaining your case in case you have one, it could be XY problem that could be solve in a different way Commented Dec 13, 2022 at 11:17
  • @EstusFlask It is not exactly a problem per se.....as I said, I could easily achieve my goal by using window.location.origin+'/' to get the base url. What I worry about is one day, a customer might come who refuses to put anything in constant.php, or change his env variable names or asks for something that cant be extracted with another function. My worry is: then what? do I have a way out or am I screwed if that happens.......... Commented Dec 13, 2022 at 11:57
  • Then your problem is unclear. You describe a hypothetic problem that others are unaware of. A customer could refuse to use environment variables and insist an app to read their mind, this seems to be the same kind of problem. What exactly are these variables and what did make you think that a customer will refuse to do something that is needed to make their product to work correctly? As for env vars, I described what's needed, you'd need to modify Vue CLI or use your own Webpack setup to use vars with any name you need Commented Dec 13, 2022 at 15:50

0

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.