打字稿中的'EventTarget'类型错误中不存在属性'文件' [英] Property 'files' does not exist on type 'EventTarget' error in typescript

查看:1204
本文介绍了打字稿中的'EventTarget'类型错误中不存在属性'文件'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的离子2应用程序访问输入文件的值,但我仍面临类型'EventTarget'上不存在属性文件的问题。
因为它在js中正常工作但在打字稿中没有。
代码如下:

I am trying to access the value of the input file from my ionic 2 application but still I'm facing the issue of property files does not exist on type 'EventTarget'. As it is properly working in js but not in typescript. The code is given below:

  document.getElementById("customimage").onchange= function(e?) {
            var files: any = e.target.files[0]; 
              EXIF.getData(e.target.files[0], function() {
                  alert(EXIF.getTag(this,"GPSLatitude"));
              });
          }

请帮我解决这个问题,因为它没有构建我的离子2应用程序。

Please help me solve this issue as it is not building my ionic 2 application.

推荐答案

e.target 属性类型取决于你所在的元素返回 getElementById(...) files 输入的属性元素: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

The e.target property type depends on the element you are returning on getElementById(...). files is a property of input element: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

在这种情况下,TypeScript编译器不知道您正在返回输入元素,而我们没有事件特定于此的类。因此,您可以创建一个类似下面的代码:

In this case, the TypeScript compiler doesn't know you are returning an input element and we dont have an Event class specific for this. So, you can create one like the following code:

interface HTMLInputEvent extends Event {
    target: HTMLInputElement & EventTarget;
}

document.getElementById("customimage").onchange = function(e?: HTMLInputEvent) {
    let files: any = e.target.files[0]; 
    //...
}

这篇关于打字稿中的'EventTarget'类型错误中不存在属性'文件'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆