简体   繁体   English

如何编辑 React 生产构建的 JSON 文件

[英]How to edit a JSON file of a React production build

In my developer build I have a JSON file with one data, I want to be able to change that data in the production build.在我的开发人员版本中,我有一个包含一个数据的 JSON 文件,我希望能够在生产版本中更改该数据。 I used npm run build to make the build.我使用 npm run build 进行构建。 And I can't find that JSON file.我找不到那个 JSON 文件。 I'm making this website for someone so I want to make it easy to change the page content by using 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>
    )
}

you can check if you are in developement or production你可以检查你是在开发还是生产

function isProduction(){
return process.env.NODE_ENV === "production"
}

then in your code然后在你的代码中

const filePath = "download/" + isProduction() ? pathToProductionFile : pathToLocalFile;

then in your scripts you need to create another build scripts with the flag "NODE_ENV=production"然后在您的脚本中,您需要使用标志“NODE_ENV=production”创建另一个构建脚本

Or setup multiple .env files, check out https://www.npmjs.com/package/dotenv for example .env.local或者设置多个.env文件,查看https://www.npmjs.com/package/dotenv例如.env.local

file=download.png

and .env.production.env.production

file=production-download.png

then in your code you can use然后在你的代码中你可以使用

process.env.file 

as a variable that holds the value of your file and will differ from local to production作为保存文件值的变量,并且会因本地和生产而异

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

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