在我的开发人员构建我有一个JSON文件与一个数据,我希望能够改变生产构建中的数据。我使用npm run build来构建。我找不到JSON文件。我正在为某人制作这个网站,所以我想通过使用JSON来轻松更改页面内容。
// src/json/download.json
{
"file" : "download.png"
}
This is how I get the json file
const file = require("../json/download.json").file
const filePath = "download/" + file;
const Nav = () => {
return (
<Link className="dcm0cujv48 abled" to={filePath} target="_blank" download={file}>
<p className="fs-14 fw-700 wspwho0gps">
<i class="fas fa-cloud-download-alt fs-14"></i>
Download
</p>
<p className="yb08fmpwlp"></p>
</Link>
)
}
你可以检查你是在开发还是在生产
function isProduction(){
return process.env.NODE_ENV === "production"
}
然后在你的代码
const filePath = "download/" + isProduction() ? pathToProductionFile : pathToLocalFile;
则需要在脚本中创建另一个构建脚本带有"NODE_ENV=production"标志
或设置多个.env
文件,查看https://www.npmjs.com/package/dotenv例如.env.local
file=download.png
和.env.production
file=production-download.png
那么在你的代码中你可以使用
process.env.file
作为保存文件值的变量,从本地到生产
将有所不同。