在 React/Typescript/Webpack 5 项目中导入 svg 时出现此错误:
Cannot find module '../path' or its corresponding type declarations.
Run Code Online (Sandbox Code Playgroud)
然后我补充道:
declare module '*.svg' {
import * as React from 'react';
export const ReactComponent: React.FunctionComponent<React.SVGProps<SVGSVGElement> & { title?: string }>;
const src: string;
export default src;
}
Run Code Online (Sandbox Code Playgroud)
到custom.d.ts文件。如果我使用 svg 作为图像,它就可以工作src。但我需要使用 svg 作为 ReactElement,因为我需要根据用户点击更改其内容。
我尝试将 svg 导入为
import DislikeIcon from '../../media/icons/dislike.svg';
const Component = () => (<> <DislikeIcon /> </>)
Run Code Online (Sandbox Code Playgroud)
然后我得到这个错误:
<data:image/svg... /> is using incorrect casing. Use PascalCase for React components, …