【问题标题】:Ionic typescript property doesn't exist on HTMLElement typeHTMLElement 类型上不存在离子打字稿属性
【发布时间】:2018-09-27 10:39:43
【问题描述】:

我使用 ionic v3 和 typescript。

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

但是我有一个错误:

“HTMLElement”类型上不存在“name_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?

谢谢!

【问题讨论】:

    标签: javascript angular typescript ionic-framework ionic3


    【解决方案1】:

    使用arrow函数访问function内部的this

    根据开发者 Mozilla 文档:

    直到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

    【讨论】:

    • @Sano,很高兴为您提供帮助。如果您觉得我的回答有用,您可以接受并投票。 :-)
    猜你喜欢
    • 1970-01-01
    • 2017-11-25
    • 1970-01-01
    • 2017-09-29
    • 1970-01-01
    • 2017-03-02
    • 2021-09-07
    • 2022-01-13
    • 2021-12-15
    相关资源
    最近更新 更多