简体   繁体   English

是否可以使用公共文件夹中的 create-react-app 提供静态文件?

[英]Is it possible to serve Static files with create-react-app from public folder?

Basically I made a (relatively) simple app for a client.基本上我为客户制作了一个(相对)简单的应用程序。 The app works and all but they keep asking for changes to the data.该应用程序工作正常,但他们一直要求更改数据。

Given the originally anticipated simplicity of the app and the fact it held static data, i did not link it to any back end.ll the data lives in a local static file with an object holding the data.鉴于应用程序最初预期的简单性以及它保存静态数据的事实,我没有将它链接到任何后端。数据位于本地静态文件中,对象保存数据。

The problem is that fle gets bundled into the buld, so if i want to just change some static data without having to rbuild, i can't!问题是 fle 被捆绑到 buld 中,所以如果我只想更改一些静态数据而不必 rbuild,我不能!

I have assets that my data file can access inside the public folder, and those are working fine.我的数据文件可以在公共文件夹中访问资产,并且这些资产运行良好。 I was trying to achieve the same idea with a js file.我试图用一个 js 文件来实现同样的想法。

I canot import from outside the /src folder.我无法从 /src 文件夹外部导入。

Is there a way i can access static data from the static folder that gets added on build somehow?有没有办法可以从以某种方式添加到构建中的静态文件夹中访问静态数据?

Yes, you can place assets in the static folder.是的,您可以将资产放置在静态文件夹中。

Docs: Using the Public Folder 文档:使用公共文件夹

  • You can reference the path in index.html with %PUBLIC_URL%/path/resource .您可以使用%PUBLIC_URL%/path/resource引用index.html %PUBLIC_URL%/path/resource
  • You can use process.env.PUBLIC_URL + '/path/resource' in javascript code.您可以在 javascript 代码中使用process.env.PUBLIC_URL + '/path/resource'

Both of these approaches are replaced at build time for your final build.这两种方法在最终构建的构建时都会被替换。

If these are javascript assets, the build will not be aware of them.如果这些是 javascript 资产,构建将不会意识到它们。 You need to structure it as an external javascript library and store them in a global variable that you can reference within your code.您需要将其构建为外部 javascript 库并将它们存储在您可以在代码中引用的全局变量中。 Then you can load that javascript library in your index.html然后你可以在你的index.html加载那个 javascript 库

Say you wanna read json file containing data, you could do it in the following way:假设您想读取包含数据的 json 文件,您可以通过以下方式进行:

class App extends Component {
  async getData() {
    const res = await fetch("/json/sample.json");
    const data = await res.text();
    console.log(data);
    return this.setState({ data });
  }

  componentDidMount() {
    this.getData();
  }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <div>{this.state.data}</div>
        </header>
      </div>
    );
  }
}

Where the json folder is created inside public folder. json文件夹是在 public 文件夹中创建的。 Anything that you put within public folder is served automatically when using create-react-app .使用create-react-app时,您放入 public 文件夹中的任何内容都会自动提供。 Hope this helps.希望这可以帮助。

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

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