【问题标题】:Module '"*.svg"' has no exported member 'ReactComponent'模块 '"*.svg"' 没有导出的成员 'ReactComponent'
【发布时间】:2020-08-12 00:12:11
【问题描述】:

我使用 CRA 创建了一个新的 React 应用程序。现在,我想创建一个导出所有图标的索引文件。为此,我将所有图标存储在 src/icons 文件夹中,并创建了一个索引文件,将我的图标导出为 React 元素。

问题是,我在尝试导出 ReactComponent 时收到以下错误:

Module '"*.svg"' has no exported member 'ReactComponent'. Did you mean to use 'import ReactComponent from "*.svg"' instead?

根据CRA's docs,这应该可以在不需要声明新模块的情况下工作。

我正在使用应该支持它的react@^16.13.1react-scripts@3.4.1

奇怪的是,当我运行代码时,我既没有遇到编译错误,也没有遇到运行时错误。当我使用<ChevronRightIcon /> 时,图标会相应显示。

【问题讨论】:

    标签: reactjs typescript svg create-react-app


    【解决方案1】:

    将声明更改为

    declare module '*.svg' {
        import React = require('react')
        export const ReactComponent: React.FC<React.SVGProps<SVGSVGElement>>
        const src: string
        export default src
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-04
      • 1970-01-01
      • 2017-05-20
      • 2017-05-07
      • 2017-08-18
      • 1970-01-01
      • 2021-07-08
      • 2017-09-09
      相关资源
      最近更新 更多