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

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

问题描述

我正在尝试从我的 ionic 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"));
              });
          }

请帮我解决这个问题,因为它不是在构建我的 ionic 2 应用程序.

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

推荐答案

e.target 属性类型取决于您在 getElementById(...).filesinput 元素的一个属性: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 编译器不知道您正在返回 input 元素,而且我们没有专门用于此的 Event 类.所以,你可以像下面的代码一样创建一个:

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天全站免登陆