代码之家  ›  专栏  ›  技术社区  ›  Sano

HtmleElement类型上不存在Ionic typescript属性

  •  -1
  • Sano  · 技术社区  · 8 年前

    我使用ionic v3和typescript。

    我想把 文件\u url 但在我检查他的宽度是否等于他的高度x 2之前。

    但是,我有一个错误:

    “HTMLElement”类型上不存在“name\u array”属性。

    这是我的代码:

    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 回复  |  直到 8 年前
        1
  •  0
  •   Sravan    8 年前

    使用 arrow 访问功能 this 内部 function

    根据开发人员Mozilla文档:

    直到 箭 函数,每个新函数定义其 own this 价值 (如果是构造函数,则为新对象,在严格模式下未定义 函数调用,如果函数作为 “对象方法”等)。事实证明,使用 object-oriented 编程风格。

    箭头函数没有自己的 这

    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