是否可以使用公共文件夹中的 create-react-app 提供静态文件? [英] Is it possible to serve Static files with create-react-app from public folder?

查看:29
本文介绍了是否可以使用公共文件夹中的 create-react-app 提供静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我为客户制作了一个(相对)简单的应用程序.该应用程序可以正常工作,但他们一直要求更改数据.

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.

问题是 fle 被捆绑到 buld 中,所以如果我只想更改一些静态数据而不必 rbuild,我不能!

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!

我的数据文件可以在公共文件夹中访问资产,并且这些资产运行良好.我试图用 js 文件实现同样的想法.

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.

我无法从/src 文件夹外部导入.

I canot import from outside the /src folder.

有没有办法从静态文件夹中访问静态数据,该文件夹以某种方式添加到构建中?

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.

文档:使用公共文件夹

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

这两种方法在最终构建的构建时都会被替换.

Both of these approaches are replaced at build time for your final build.

如果这些是 javascript 资产,构建将不会意识到它们.您需要将其构建为外部 javascript 库并将它们存储在您可以在代码中引用的全局变量中.然后你可以在你的 index.html

If these are javascript assets, the build will not be aware of them. You need to structure it as an external javascript library and store them in a global variable that you can reference within your code. Then you can load that javascript library in your index.html

这篇关于是否可以使用公共文件夹中的 create-react-app 提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆