javascript - 尝试在 vue.js 中使用 clear() 清除表单时出现 Typescript 错误
问题描述
当用户单击重置按钮时尝试重置表单时出现打字稿错误。
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。而不是强制转换和抑制错误,您宁愿使用类型保护:
if (this.$refs.searchForm instanceof HTMLFormElement) {
this.$refs.searchForm.clear();
}
推荐阅读
- javascript - 如何在 JS & CSS Minifier 的 VSCode (Win10) 中启用保存缩小
- c# - C# 为什么我的 DataGridView 在选项卡控件中的列索引随机更改
- c++ - 如何从翻译中过滤掉快捷方式
- typescript - Typescript 编译器类型到类
- javascript - EventListener keydown 事件未触发多个键按下
- python - 如何让 models.UUIDField 字段没有破折号
- electron - 如何在浏览器中打开电子应用程序作为本地网站
- c# - 如何替换集合中的元素
- jquery - html5 验证 - 当值不等于某个字符串时有效
- java - JUnit - 编写测试