打字稿错误:'HTMLElement'类型中不存在属性'文件'

Jer*_*rry 7 typescript ionic-framework angular

我希望使用IONIC为我的应用程序创建上传功能.

这是我的HTML代码:

<input ion-button block type="file" id="uploadBR">
<input ion-button block type="file" id="uploadIC">
<button ion-button block (click)="upload()">Confirm Claim Restaurant</button>
Run Code Online (Sandbox Code Playgroud)

这是我的upload()功能:

upload(){   
   let BR = document.getElementById('uploadBR').files[0]
   let IC = document.getElementById('uploadIC').files[0]
   console.log(BR)
   console.log(IC)    
 }
Run Code Online (Sandbox Code Playgroud)

在普通的HTML中它应该可以工作,但它不适用于我的IONIC.

构建应用程序时,它将显示错误 Typescript Error: Property 'files' does not exist on type 'HTMLElement'.

我是以错误的方式做到的,还是必须以打字稿的不同方式完成?

谢谢.

ran*_*082 20

您需要将其转换为HTMLInputElement,因为它filesinput元素的属性

let BR = (<HTMLInputElement>document.getElementById('uploadBR')).files[0];
Run Code Online (Sandbox Code Playgroud)


小智 5

在 React.js 项目中,您可能需要使用“as”进行转换,因为该语法在文件<>中不起作用.tsx

const file = (document.getElementById(id) as HTMLInputElement).files[0];
Run Code Online (Sandbox Code Playgroud)