简体   繁体   English

如何获取目录中的所有文件?

[英]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.

相关问题 如何使用 React 获取目录中所有文件的名称列表? - How do you get a list of the names of all files in a directory using React? 运行目录中的所有文件 - Run all files in a directory 获取 Node.js 目录中所有服务器文件的列表 - Get List of all server files in a directory in Node.js 如何从 React 中的目录导入和播放所有音频文件? - How to import and play all audio files from a directory in React? 如何使用 parcel-bundler 将公共目录中的所有文件添加到构建目录 - How can add all files in public directory to the build directory with parcel-bundler 如何让 VSCode 不检查目录中的 other.ts 文件是否存在变量冲突? - How to get VSCode to not check other .ts files in directory for variable conflicts? 如何通过 Microsoft Graph - React 让所有用户进入活动目录? - How to get all users in active directory via Microsoft Graph - React? Javascript / Webpack:如何使用自定义循环将文件中的所有json文件连接起来 - Javascript / webpack : how to concatenate all json files in directory with a custom loop over files 如何将所有反应生产构建文件放在单级根文件夹中而不将目录树保持为静态 - How to put all react production build files in single level root folder without keeping directory tree in static 如何导出目录中的所有文件以便我可以将它们与点运算符一起使用? - how to export all files in a directory so that i can use them with dot operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM