1

当用户单击重置按钮时尝试重置表单时出现打字稿错误。

onClearSearchFormClicked() {
  this.$refs.searchForm.reset();
}
197:27 Property 'reset' does not exist on type 'Element | Element[] | Vue | Vue[]'.
  Property 'reset' does not exist on type 'Element'.
    196 |   onClearSearchFormClicked() {
  > 197 |     this.$refs.searchForm.reset();
        |                           ^
    198 |   }
Version: typescript 3.8.3
4

2 回答 2

2

我不会建议其他答案。TypeScript 正确地告诉您该对象不能是 HtmlFormElement。而不是强制转换和抑制错误,您宁愿使用类型保护

if (this.$refs.searchForm instanceof HTMLFormElement) {
  this.$refs.searchForm.clear();
}
于 2020-04-20T22:10:11.207 回答
0

这是有效的代码。

  onClearSearchFormClicked() {
    //this.$refs.searchForm.reset();
    (this.$refs.searchForm as HTMLFormElement).reset();
  }
于 2020-04-20T20:51:16.267 回答