Reactjs Can';无法获取HtmleElement的值

Reactjs Can';无法获取HtmleElement的值,reactjs,typescript,ionic5,tsx,Reactjs,Typescript,Ionic5,Tsx,我目前正在使用Ionic React处理.tsx文件。当我执行一个简单的文档.getElementById(“输入id”).value时,得到一个错误属性“value”在类型“HTMLElement”上不存在。为了找到解决这个问题的方法,使用typescript解析器,但是不起作用,因为HtmleElement没有很多HTMLInputElement道具 在此上下文中,如何获取输入的值 我的密码在这里。由于Typescript错误,它甚至无法编译: handleChange() { cons

我目前正在使用Ionic React处理.tsx文件。当我执行一个简单的
文档.getElementById(“输入id”).value
时,得到一个错误
属性“value”在类型“HTMLElement”上不存在
。为了找到解决这个问题的方法,使用typescript解析器
,但是不起作用,因为HtmleElement没有很多HTMLInputElement道具

在此上下文中,如何获取输入的值

我的密码在这里。由于Typescript错误,它甚至无法编译:

handleChange() {
  const input = document.getElementById("id").value;
}

render() {
  return (
    <IonPage>
      <IonContent fullscreen>
        <TextField
          id="id"
          label="Input"
          variant="outlined"
          required
        />
      </IonContent>
    </IonPage>
  );
}
handleChange(){
常量输入=document.getElementById(“id”).value;
}
render(){
返回(
);
}

您可以使用带有
作为关键字的强制转换,并声明一个您知道属于
HTMLInputElement
类型的变量。然后访问其
属性

const inputElement: HTMLInputElement = document.getElementById("input-id") as HTMLInputElement;
console.log(inputElement.value)

我在TypeScript平台上尝试了这段代码,没有出现任何类型错误。它起作用了~aaaahh我现在非常高兴~谢谢,先生