用.env反应环境变量 [英] React env variables with .env
本文介绍了用.env反应环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试遵循有关从react-create-app添加env变量的文档,但未成功:
I'm trying to follow docs on adding env variables from react-create-app without success:
- 在文档的根目录中,我有一个".env"文件(默认的.env属性)
- .env文件仅包含一个变量'REACT_APP_API_HOST = http://localhost:8080 '
- 尝试访问我的应用程序(通过create react app创建)中的process.env给我未定义
这是app.js,我正在其中尝试访问process.env而不成功.
This is app.js where I'm trying to access process.env without success.
我无法在代码内访问process.env.有没有可行的例子呢?
I can't access process.env inside the code. Is there any working example on how to do it?
推荐答案
您可以在根目录上使用.env文件.
You can use .env file on root.
-
start: react-scripts start
-使用.env.development
-
build_staging: "set REACT_APP_ENV=staging & react-scripts build"
-使用.env.staging
-
build: "react-scripts build"
-使用.env.production
start: react-scripts start
- uses.env.development
build_staging: "set REACT_APP_ENV=staging & react-scripts build"
- uses.env.staging
build: "react-scripts build"
- uses.env.production
这篇关于用.env反应环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文