下面一行在tsx檔案中拋出上述錯誤
import MySvgImg from "images/mySvgImage.svg";
<img src={MySvgImg} alt="This line throws error" />
我在中處理了 SVG 匯入global.d.ts
declare module "*.svg" {
const ReactComponent: any;
export const ReactComponent;
}
我還在我的tsconfig.json
"include": ["src/**/*", "global.d.ts"],
uj5u.com熱心網友回復:
您可以將 SVG 作為組件匯入。
import { ReactComponent as MySvgImg } from "images/mySvgImage.svg";
通過此匯入,您可以將 SVG 圖示用作普通的 React 組件
<MySvgImg />
global.d.ts如果您收到一個新的錯誤提示沒有命名的匯入,您可能需要更改。
declare module "*.svg" {
const ReactComponent: any;
export { ReactComponent };
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/536436.html
