typescript - TypeScript - 找不到名称“图像”
问题描述
我正在尝试创建一个图像加载类,但在数组定义中它会抛出一个错误name "Image" not found
,尽管稍后在代码中创建了一个图像对象。
class ImageLoader {
static toLoadCount:number = 0;
private static images = Array<Image>();
static onAllLoaded() {};
static onImageLoad() {
--Images.toLoadCount;
if(Images.toLoadCount == 0)
Images.onAllLoaded();
}
static load(path: string) {
++Images.toLoadCount;
let img = new Image();
img.src = "Images/" + path;
img.onload = Images.onImageLoad;
Images.images[path.substring(0, path.length - 4)] = img;
return img;
}
static allImagesLoaded() {
return (Images.toLoadCount == 0);
}
[key: string]: any;
}
解决方案
不幸的是,我无法发表评论,因为我没有足够的代表,但我只想在 Aleksey L 的解决方案中再添加一件事。
通常,如果您使用 VS-Code 或 Webstorm 或任何具有智能感知功能的编辑器,您可以将鼠标悬停在“ new Image()
”上,它会告诉您它的类型。
所以在这种情况下,你会知道 img 实际上是一个 HTMLImageElement:
let img: HTMLImageElement = new Image();
然后您会立即意识到您的数组不正确,因为图像不是type Image
类型而是类型HTMLImageElement
,您可以立即将数组调整为HTMLImageElement[]
.
克鲁,乔治
PS:如果您对此表示赞同,我会很高兴,我讨厌写这么长的帖子而不是简短的评论。
推荐阅读
- python - 如何使用python在列中加入csv和求和值
- bootstrap-4 - 如何删除引导程序 4 中 pre 标签中引入的边距?
- python - 如何从视图中的异步事件循环中获取任务?
- reactjs - 将状态从一个组件传递到另一个组件的正确方法
- swift - @EnvironmentObject 符合协议:Xcode 未编译
- kubernetes - 将 Minikube 连接到 Sysdig 时“等待第一个节点连接...”
- c# - 如何在 asp mvc 中更新 MDB 管理模板资产?
- javascript - 在脚本 src 中传递变量
- angular - Angular 7 - 在 Azure AD 身份验证后获取登录用户名
- sql - 将学生的分数转换为相应的成绩