HTMLElement类型不存在离子打字稿属性

时间:2018-04-17 12:44:02

标签: javascript angular typescript ionic-framework ionic3

我使用离子v3和打字稿。

我想在 file_url 中添加数组,但在检查他的宽度是否等于他的身高x 2之前。

但是我有一个错误:

  

' name_array' “HTMLElement”中不存在属性'类型。

这是我的代码:

let img = new Image();

img.src = file_url;

img.onload = function () {

    if( img.width == (img.height * 2) ){
        this.name_array.push({ id: 1, url: file_url });
    }

};

如何在我的数组中推送文件的URL?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用arrow功能访问this

中的function

根据开发人员Mozilla docs:

  

arrow函数之前,每个新函数都定义了 own this 值   (构造函数中的新对象,在严格模式下未定义)   函数调用,如果函数被调用为基础对象   "对象方法"等)。事实证明这不太理想   object-oriented编程风格。

箭头功能没有自己的this

let img = new Image();
img.src = file_url;
img.onload = () => {
   if( img.width == (img.height * 2) ){
     this.name_array.push({ id: 1, url: file_url });
   }
};

Here is the documentation for the same

相关问题