[英]How to get all files in a directory?
I have a number of files in a folder in React.我在 React 的一个文件夹中有许多文件。 Each of them must be self-initialized.它们中的每一个都必须自初始化。 For this, I need to import them.为此,我需要导入它们。 I wouldn't want to import each file individually.我不想单独导入每个文件。 I need to get a list of all the files.我需要获取所有文件的列表。 But we do not have access to FS on the client.但是我们无法访问客户端上的 FS。 I tried this https://github.com/diegohaz/list-react-files solution, but it does not work (it looks like it somehow uses fs, and gives an error).我尝试了这个https://github.com/diegohaz/list-react-files解决方案,但它不起作用(看起来它以某种方式使用了 fs,并给出了错误)。 Can I solve my problem in a straightforward way?我能以直接的方式解决我的问题吗? Can this be done using the web-pack?这可以使用 web-pack 来完成吗? Any ideas guys?有什么想法吗?
Yes, we don't have fs on the client so we need to each file manually.是的,我们在客户端上没有 fs,所以我们需要手动创建每个文件。 but there is an easy way of doing it.但有一种简单的方法可以做到。
Make a registry file where you put the path of file from registry.js relative制作一个注册表文件,在其中放置 registry.js 相对文件的路径
{
"file1": "/folder1/sda.png",
"file2": "/folder2/asd.png"
}
Now you don't need to import each file manually import registry.json现在你不需要手动导入每个文件 import registry.json
import registry from registry.json
for x in registry {
try {
require(`path to registry/${registry[x]}`)
} catch (e) {
console.log(e)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.