javascript - 如何在索引文件中动态导入/导出组件?
问题描述
如何在 index.js 文件的文件夹中动态导入和导出所有模块?我正在使用 ReactJS 并希望在编译应用程序时(而不是在运行时)导入/导出。
前
// index.js
import ComponentA from './ComponentA';
import ComponentB from './ComponentB';
import ComponentC from './ComponentC';
export { ComponentA, ComponentB, ComponentC }
之后 - 但不起作用
// index.js
const fs = require('fs');
let components = {};
fs.readdirSync(__dirname)
.filter((f) => f !== 'index.js')
.forEach((file) => {
if (file.toLowerCase().endsWith('.js')) {
filePath = './'+file;
fileName = file.split('.')[0];
// Not sure how to write this part properly
components[fileName] = import(filePath);
}
});
export components; // SyntaxError: Unexpected token 'export'
解决方案
推荐阅读
- html - 我怎样才能使按钮正确,修复中间部分并使所有内容都可以调整大小
- javascript - 使用动态模式验证时,响应式表单始终无效
- rasa - 如何以 rasa 形式启动 rasa 动作
- php - 如果我为已在购物车中的产品单击“添加到购物车”按钮,如何重定向到购物车页面
- reactjs - 无法使用 Redux 加载数据
- amazon-dynamodb - DynamoDB 建模问题:玩家 1 和玩家 2 之间的游戏表,如何获取涉及给定玩家的所有游戏
- selenium - 如何在铬的内部页面上获取/与 element_locator 交互
- javascript - 扫描网站以查找重复的关键字并创建一个数组
- php - 如何创建正则表达式以将字符串完全拆分为 3 部分?(PHP)
- docker - dotnet core 2.1 aspnetcore_environment 通过 docker 更改不工作