gpt4 book ai didi

reactjs - TypeScript - 模块 '"*.svg "' has no exported member ' ReactComponent

转载 作者:搜寻专家 更新时间:2023-10-30 21:03:22 28 4
gpt4 key购买 nike

我正在尝试使用 TypeScript 将 .svg 文件导入为 React 组件。

根据 React 文档,这样做是这样的:

从 './Icon.svg' 导入 { ReactComponent as Icon };

在 TypeScript 文档之后,我添加了这个:

// custom.ts.d

declare module '*.svg' {
const content: any;
export default content;
}

// tsconfig.json

{
...
"files": [
"custom.d.ts"
]
}

SVG 正在渲染。但是我遇到了 TypeScript 错误:

[ts] 模块“*.svg”没有导出成员“ReactComponent”。 [2305]

如果有帮助,这是我的完整 tsconfig 文件:

{
"compileOnSave": false,
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./dist",
"strict": true,
"jsx": "react",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"skipLibCheck": true,
"esModuleInterop": true,
"baseUrl": ".",
"plugins": [
{
"name": "typescript-styled-plugin"
}
],
"typeRoots": ["./node_modules/@types"],
"lib": ["dom", "es2015", "es2017"]
},
"exclude": ["node_modules", "dist"],
"files": [
"custom.d.ts"
]
}

谢谢!

最佳答案

您已经导出默认内容;但是您正在进行命名导入(不是默认导入)。

修复

更改声明以导出您正在导入的名称:

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

附加

建议不要在 tsconfig.json 中使用 files。而只是使用 include

关于reactjs - TypeScript - 模块 '"*.svg "' has no exported member ' ReactComponent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54121536/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com