首页 前端知识 Vue TypeScript开发中TS不识别this.$refs的问题

Vue TypeScript开发中TS不识别this.$refs的问题

2024-06-24 02:06:03 前端知识 前端哥 767 430 我要收藏

用iview框架开发后管系统,需要用到form表单功能,最后提交表单代码是:

this.$refs.form.validate(......)

代码报错

Property 'validate' does not exist on type 'Vue | Element | Vue[] | Element[]'.
Property 'validate' does not exist on type 'Vue'

既然TS不知道this. r e f s 是什么,那么我们需要给它加上类型断言,告诉它是什么,也就是需要把 t h i s . refs是什么,那么我们需要给它加上类型断言,告诉它是什么,也就是需要把 this. refs是什么,那么我们需要给它加上类型断言,告诉它是什么,也就是需要把this.refs[formName] 显式标注为你的那个组件类型;
我项目中的解决方案是:

 ;(this.$refs['form'] as HTMLFormElement).validate(...)
转载请注明出处或者链接地址:https://www.qianduange.cn//article/13376.html
标签
评论
发布的文章

jQuery 选择器

2024-05-12 00:05:34

Vue中public/assets目录区别

2024-07-02 23:07:29

大家推荐的文章
会员中心 联系我 留言建议 回顶部
复制成功!