React版本: "react": "^18.2.0"
1、在package.json平级目录下创建'.env'文件
2、在‘.env’文件里配置环境变量
【1】PUBLIC_URL
描述:编译时文件的base-href
官方描述:
// We use `PUBLIC_URL` environment variable or "homepage" field to infer
// "public path" at which the app is served.
// webpack needs to know it to put the right <script> hrefs into HTML even in
// single-page apps that may serve index.html for nested URLs like /todos/42.
// We can't use a relative path in HTML because we don't want to load something
// like /todos/42/static/js/bundle.7289d.js. We have to know the root.
const publicUrlOrPath = getPublicUrlOrPath(
process.env.NODE_ENV === 'development',
require(resolveApp('package.json')).homepage,
process.env.PUBLIC_URL
);
配置方式:
PUBLIC_URL = /zyk
效果:
编译后的文件都增加了‘/zyk’前缀
【2】BUILD_PATH
描述:编译后文件夹名称,默认是‘build’
配置方式:
BUILD_PATH = 'dist'
效果:文章来源:https://www.toymoban.com/news/detail-646535.html
文章来源地址https://www.toymoban.com/news/detail-646535.html
到了这里,关于React如何配置env环境变量的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!