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

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

最佳答案

我不建议其他答案。 TypeScript正确地告诉您该对象不能是HtmlFormElement。与其强制转换和抑制错误,不如使用type guard

if (this.$refs.searchForm instanceof HTMLFormElement) {
  this.$refs.searchForm.clear();
}

关于javascript - 尝试在vue.js中使用clear()清除表单时出现Typescript错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61331727/

10-11 06:56