【问题标题】:Creating a barrel file in react (using .tsx)在反应中创建桶文件(使用.tsx)
【发布时间】:2020-05-06 01:00:24
【问题描述】:

这是我的文件夹结构:

  • 源
    • 资产
    • 组件
    • 型号
      • counter.tsx
      • index.ts

models/index.tsx(桶文件)内的代码

export * from "./counter";

models/counter.tsx 中的代码

export default interface ICounter {
  id: number;
  value: number;
}

进口声明

import { ICounter } from "models";

问题是我的导入语句出现错误,说我的桶文件没有导出成员ICounter

如何在 .tsx 中创建桶文件?

【问题讨论】:

  • 您如何解决'models' 导入问题?不应该是相对路径吗?
  • 1.请附上models/index.tsx 和counter.tsx 的完整来源。 2.请不要将错误文字作为图片发布。
  • 我在 tsconfig.json 的 compilerOptions 下有一个 baseUrl: "src" 属性
  • 你如何在counter.tsx 中导出ICounter?是命名导出吗?
  • 您将ICounter 导出为default 并以命名方式导入。桶必须命名。从ICounter 导出中删除default。

标签: reactjs typescript tsx


【解决方案1】:

创建文件index.ts

export { blue } from './blue';
export { yellow} from './yellow';
export { green} from './green';
export { red} from './red';

来源:https://hackernoon.com/react-project-architecture-using-barrels-d086146eb0f6

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 2021-06-26
    • 2018-02-23
    • 1970-01-01
    相关资源
    最近更新 更多