【发布时间】:2019-09-24 18:25:57
【问题描述】:
我正在尝试将相同的图像加载到我的 HTML 中的所有画布中。我以前用 javascript 编写了整个网页,但现在我正在学习 angular 和 typescript。
我在 Visual Studio Code 中突出显示了这个错误:
“HTMLElement”类型的参数不能分配给“CanvasImageSource”类型的参数。 “HTMLElement”类型缺少“HTMLVideoElement”类型的以下属性:height、msHorizontalMirror、msIsLayoutOptimalForPlayback、msIsStereo3D 和 80 more.ts(2345)
但我没有在控制台中显示任何错误。我可以让图像加载到画布内的 HTML 上(所以它正在工作)。
这是我的 ts:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.scss']
})
export class MapComponent implements OnInit {
constructor() { }
ngOnInit() {
document.querySelectorAll("canvas").forEach(c=>{
var ctx = c.getContext("2d");
var img = document.getElementById("P43");
ctx.drawImage(img,0,0,106,50); //(image, offsetx, offsety, x, y)
});
}
}
VS Code 突出显示此行 ctx.drawImage(img,0,0,106,50); //(image, offsetx, offsety, x, y) 上的错误,它突出显示 img 并显示错误。
我试图搜索这个,但我找到的唯一解决方案是当你有标签的 ID 时(我没有,因为它在页面上都是画布。)或者如果有输入。
感谢任何帮助。
【问题讨论】:
-
id P43 的元素是画布还是图像元素?
-
您需要使用类型保护缩小
img的类型或强制转换它。
标签: angular typescript visual-studio-code