"export"转换为"export default"以防止导入



我正在尝试用TypeScript和Parcel写一个库。一切都很好,直到我尝试在另一个应用程序中导入它。

库中有一个index.ts文件,它收集组件并导出它们:

import Component1 from "./components/Component1";
import Component2 from './components/Component2';
export {
Component1,
Component2,
};

构建后得到index.js,index.cssindex.d.ts。在index.d.ts文件中,export已被转换为export default:

export default Component1;
export default Component2;

我将我的库与纱线链接到我的消费者应用程序。当我想要import { Component1 } from 'myLibrary';时,我得到以下错误:Module '"myLibrary"' has no exported member 'Component1'. Did you mean to use 'import Component1 from "myLibrary"' instead?。现在,当我尝试导入默认的(就像它在index.d.ts)import Component1 from 'myLibrary';错误更改为:Attempted import error: 'myLibrary' does not contain a default export (imported as 'Component1').

为什么export转换发生,我如何规避这一点?

编辑:库是按包构建和捆绑的,消费者是由反应脚本处理的。曼哈顿先生的建议后:

index.ts:

export {Component1} from "./components/Component1";
export {Component2} from './components/Component2';

生成index.d.ts:

export {Component1} from "./components/Component1";
export {Component2} from './components/Component2';

消费者:

function App() => {
render(
<>
<Component1 />
<Component2 />
</>
)
};

错误:

./src/App.tsx
[1] Attempted import error: 'Component2' is not exported from 'myLibrary'.

可以直接导出:

export Component1 from "./components/Component1";
export Component2 from './components/Component2';

您正在尝试重新导出一个不存在的命名导出。尝试重新导出默认值:

export { default as Component1 } from './components/Component1';
export { default as Component2 } from './components/Component2';

相关内容

  • 没有找到相关文章

最新更新