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 引用资源文件后提示找不到的异常处理的文章就介绍到这了,更多相关TypeScript 引用资源文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
使用getBoundingClientRect方法实现简洁的sticky组件的方法
本文介绍这种组件的实现思路,并提供一个同时支持将sticky元素固定在顶部或底部的具体实现,由于这种组件在网站中非常常见,所以有必要掌握它的实现方式,以便在有需要的时候基于它的思路写出功能更多的组件出来2016-03-03
最新评论