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

用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(...)

猜你喜欢

转载自blog.csdn.net/weixin_44834981/article/details/128640078
今日推荐