首页 > 解决方案 > React import svg 出现 TS 错误:“找不到模块 '../images/bar.svg'。”

问题描述

.tsx文件中,我可以导入.svg文件,设置以下tsconfig.jsonindex.d.ts文件。

tsconfig.json

"compilerOptions": {...},
"include": ["./app/src/index.d.ts"]

索引.d.ts

declare module "*.svg" {
  const content: string;
  export default content;
}

然后我像这样导入 import image from "../images/bar.svg";

没有webpack编译错误,使用镜像也没有问题,但是刚刚从vscode得到了ts错误: Cannot find module '../images/bar.svg'.

如何消除此错误?

标签: reactjstypescriptsvgimport

解决方案


推荐阅读