打字稿:类型“ HTMLElement”上不存在属性“ src”

比约恩·约瑟斯(BjörnHjorth):

我从Typescript中收到一个错误,我不确定如何纠正它。“编译”时,代码工作正常,但我无法更正错误。我从代码中提取了涉及错误的部分。我想我必须预先定义src,但不确定如何。

在编辑器中和Gulp上编译时出现错误消息:

“属性'src'在类型'HTMLElement'上不存在。在第53行第17行”

...

element:HTMLElement; /* Defining element */

'''
this.element = document.createElement('img'); /*creating a img*/

'''

这是我运行的用于渲染元素,位置,顶部和左侧的所有方法而没有给出错误的方法。

display() {
   this.element.src = this.file; /*This is the line that gives the error*/
   this.element.style.position = "absolute";
   this.element.style.top = this.pointX.toString() + "px";
   this.element.style.left = this.pointY.toString() + "px";

   document.body.appendChild(this.element);
};
约翰·威兹(John Weisz):

因为src不是HTMLElement类型的属性,而是的HTMLImageElement

如果确定会得到一个img元素,则可能需要使用正确的子类型声明变量:

element: HTMLImageElement; /* Defining element */

// ...

this.element = document.createElement('img'); /*creating a img*/

另外,您可能想看看document.createElement返回的结果。如果将"img"指定为参数,则它是相同类型

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

忽略打字稿错误“类型的值不存在属性”

类型“ HTMLElement”上不存在“已检查”属性。

HTMLElement类型不存在selectedIndex属性

属性在类型上不存在。打字稿

打字稿认为getYear在日期类型上不存在

打字稿“元素类型上不存在属性”

类型“ HTMLElement”上不存在属性“ toDataURL”

TS2339:类型“ HTMLElement”上不存在属性“ on”

打字稿错误:类型“ HTMLElement”上不存在属性“ append”

打字稿错误-类型上不存在属性“权限”

属性“ checked”在类型“ HTMLElement” angular 4上不存在

打字稿错误:类型“ HTMLElement”上不存在属性“文件”

类型“ HTMLElement”上不存在属性“ pseudoStyle”

“播放”在类型“ HTMLElement”上不存在,属性“值”在类型“ EventTarget”上不存在

属性“值”在类型“ HTMLElement”上不存在

打字稿:“ typeof aceAjax”类型不存在属性require

打字稿-主题类型上不存在属性扫描...?

如何用联合类型修复打字稿“类型上不存在属性”?

打字稿:类型'Global'不存在属性'DB'

类型“ HTMLElement”上不存在属性“ connectedCallback”

属性“ noUiSlider”在类型“ HTMLElement”上不存在

打字稿-类型“ HTMLElement”上不存在属性“ scrollY”

类型“HTMLElement”上不存在打字稿错误属性。任何

“HTMLElement”类型上不存在属性“getContext”

打字稿:“从不”类型上不存在“title”属性

打字稿类型错误属性不存在

打字稿:“从不”类型上不存在“用户”属性

打字稿错误“类型'HTMLElement'上不存在属性'值'”,“类型'元素'上不存在属性'onclick'”

“HTMLElement”类型上不存在属性 {created in html element}