TypeScript中引用资源文件后提示找不到怎么办
这篇文章主要为大家展示了TypeScript中引用资源文件后提示找不到怎么办,内容简而易懂,下面让小编带大家一起学习一下吧。
在tsx中引用图片,在文件文本编辑器中提示错误引用:
typescript无法识别非代码文件(js是可以的)。如果需要在ts中识别此文件资源,可以先声明文件类型。
新建一个ts文件,比如global.d.ts(.d.ts是typescript declaration file的简称),并放在主要代码文件夹下。
在ts文件中,添加各种文件类型的声明,比如:
declare module '*.svg' { interface Svg { content: string; id: string; viewBox: string; node: any; } const svg: Svg; export default svg;}declare module '*.png' { const png: string; export default png;}declare module '*.mp3' { const mp3: string; export default mp3;}declare module '*.gif' { const png: string; export default png;}
也可以简略为declaremodule'*.png';
项目编译时,会自动读取文件内容。然后就能识别资源文件了
以上就是关于TypeScript中引用资源文件后提示找不到怎么办的内容,如果你们有学习到知识或者技能,可以把它分享出去让更多的人看到。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。