是否可以通过public文件夹中的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.
问题在于,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
这篇关于是否可以通过public文件夹中的create-react-app服务静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!