在 React/Nextjs 的文件夹中导入多个文件 [英] Import multiple files in folder in React / Nextjs

查看:25
本文介绍了在 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 的文件夹中导入多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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