本文介绍了在React/Nextjs的文件夹中导入多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在文件夹中导入多个具有特定扩展名的文件:
I'm trying to import multiple files with a certain extension in a folder:
const allEntries = require.context('../static/blog', true, '/\.md/')
但是我得到了:
Unhandled Rejection (TypeError): __webpack_require__(...).context is not a function
我正在使用 Nextjs ,并且需要其中一个页面中的文件.这里似乎有东西吗?
I'm using Nextjs and require the files in one of the pages. Something seems off here?
我不一定需要通过require
进行操作,我只希望能够一次导入/需要多个文件,而无需知道文件名或文件数量.在文件夹中.
I don't necessarily need to do it via require
I just want to be able to import/require multiple files at once without knowing the filename or how many of the files are in a folder.
推荐答案
据我了解,您非常接近,从错误中您正在使用webpack的 require.context
From what I understand you are very close, from the error you are using webpack's require.context
const allEntries = require.context('../static/blog', true, '/\.md/')
console.log(allEntries.keys()) // all the files found in the context
allEntries.keys().forEach(allEntries) // require them all
这篇关于在React/Nextjs的文件夹中导入多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!