简体   繁体   English

如何在.js文件中使用Vue .env设置?

[英]How can I make use of Vue .env settings in a .js file?

I have a .js file in which I would like to read the value of a Vue environment variable, as set via a .env.* file. 我有一个.js文件,我想在其中读取通过.env。*文件设置的Vue环境变量的值。

In my main.js file, where the Vue instance is created, I can read those variables/settings via "process.env. ...". 在创建Vue实例的main.js文件中,我可以通过“ process.env。...”读取这些变量/设置。 However, in this other .js file, I cannot find a way to read them there. 但是,在这个其他.js文件中,我找不到在那里读取它们的方法。 No "process.env. ..." variables are visible there. 在此处看不到“ process.env。...”变量。 And I also cannot find them via the "this. " object or the "Vue. " object (whichever is set at that point in the code). 而且我也无法通过“ this。 ”对象或“ Vue。 ”对象(以代码中当时设置的那个为准)找到它们。 Eg not via something like "Vue.process.env. ...". 例如,不通过“ Vue.process.env。...”之类的东西。 I import Vue at the beginning of this file via: 我通过以下命令在该文件的开头导入Vue:

 import Vue from 'vue';

but that doesn't help in any way, it seems. 但这似乎没有任何帮助。

How can I read these variables there? 我如何在那里读取这些变量?

process.env is accessible only in the node environment. process.env仅在节点环境中可访问。 If you want to use it in your client side code, you can use webpack.DefinePlugin; 如果要在客户端代码中使用它,则可以使用webpack.DefinePlugin;。 in Vue Cli 3, this is automatic for all variables that start with VUE_APP_: Env Variables Vue CLI 3 在Vue Cli 3中,这对于以VUE_APP_开头的所有变量都是自动的: Env Variables Vue CLI 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM