我试图将SVG作为带有VTE和vite-plugin-svgr
插件的ReactComponent导入,但我遇到了一个错误,我认为该错误与类型有关.
下面是我的IMPORT语句和用法:
import { ReactComponent as ProfileIcon } from '../assets/profile.svg'
export function Example() {
return (
<div>
<p>blahblahblah</p>
<ProfileIcon />
<div>
}
}
该页面根本不会加载,并且我的浏览器控制台显示以下错误:
未捕获语法错误:不明确的间接导出:ReactComponent
我认为这与我的custom.d.ts文件有关,其中包含以下声明:
declare module '*.svg' {
import * as React from 'react'
export const ReactComponent: React.FunctionComponent<
React.ComponentProps<'svg'> & { title?: string }
>
export default ReactComponent
}
我在排除了另一个打字错误后添加了这个声明,许多StackOverflow用户发布了同样的建议,但只有here's one example个.
我似乎找不到适合我的Reaction+Vite+TypeScrip SVG组件指南.
以下是我做的其他一些更改,以供参考:
tsconfig.json个
{
"compilerOptions": {
...
"types": ["vite-plugin-svgr/client"]
},
...
"include": ["src", "custom.d.ts"]
}
vite-env.d.ts个
/// <reference types="vite-plugin-svgr/client" />
/// <reference types="vite/client" />