Javascript 动态导出文件后,typescript找不到模块[File';/index.tsx';不是模块。ts(2306)]
我尝试动态导出项目中的所有组件。但是tsl不允许我传递和抛出错误消息Javascript 动态导出文件后,typescript找不到模块[File';/index.tsx';不是模块。ts(2306)],javascript,node.js,typescript,ecmascript-6,Javascript,Node.js,Typescript,Ecmascript 6,我尝试动态导出项目中的所有组件。但是tsl不允许我传递和抛出错误消息文件'/src/components/index.tsx'不是模块。ts(2306) 如何防止此错误并使其了解这些是动态导出模块 这是我的文件夹结构 在/src/components/index.tsx中,我尝试动态导出所有组件 const cache: any = {}; function getAllComponents() { req.keys().forEach((key) => { if (!(/^\
文件'/src/components/index.tsx'不是模块。ts(2306)
如何防止此错误并使其了解这些是动态导出模块
这是我的文件夹结构
在/src/components/index.tsx中,我尝试动态导出所有组件
const cache: any = {};
function getAllComponents() {
req.keys().forEach((key) => {
if (!(/^\.\/index.tsx$/.test(key))) {
const regex = /^\.\/([^\/]+)[\/].+/;
const found = key.match(regex)[1];
const newKey = found.split('-')
.map(name => name.charAt(0).toUpperCase() + name.slice(1).toLowerCase())
.join('');
cache[newKey] = req(key).default;
}
});
return { ...cache, __esModule: true };
}
module.exports = getAllComponents();
在/src/index.tsx中,我尝试从components文件夹导出所有组件
export * from './components/index';
它在/src/index.tsx中的“./components/index”的这一行
export*上抛出一个错误。
错误消息:文件“/src/components/index.tsx”不是模块。ts(2306)您必须在源文件ie/src/components/index.tsx中导出,并在/src/index.tsx.Hi Ramkumar p中导入,谢谢您的回复。正如我在文章中所写,我已经在/src/components/index.tsx中动态导出了我的所有组件。根据此处发布的github问题,Typescript目前不了解动态导出模式
export * from './components/index';