简体   繁体   English

在 react.js 中获取未定义的环境变量

[英]Getting env varibles as undefined in react.js

        const { URL } = process.env;
        alert(URL);
        export const API_URL = URL;

my consants File.我的意见档案。 Here i am using backend url from.env File.这里我使用的是来自.env 文件的后端 url。 But i am getting undefined when alert.但是我在警报时变得不确定。

    URL=http://statrix.ru

Here is my.env File.这是 my.env 文件。

Please let me know whats wrong i am doing请让我知道我在做什么错

If you are using create-react-app to initialize your project so I think you should put prefix REACT_APP before your environment variable.如果您使用 create-react-app 来初始化您的项目,那么我认为您应该在环境变量之前放置前缀REACT_APP

In this case, that should be在这种情况下,应该是

REACT_APP_URL=http://statrix.ru

and to get this variable in your javascript that should be并在您的 javascript 中获取此变量,这应该是

const { REACT_APP_URL } = process.env;

For more your information, please read official document at here如需更多信息,请阅读此处的官方文档

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

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