vue+ts使用$refs属性报错 Property ‘focus’ does not exist on type ‘Vue | Element | Vue[] | Element[]’.

项目是使用vue+typescript,代码如下:
this.$refs.mainInput.focus()
报错Property ‘focus’ does not exist on type ‘Vue | Element | Vue[] | Element[]’. Property ‘focus’ does not exist on type ‘Vue’.
报错了,是不能正常打包的,必须要把问题解决了
解决方法如下:
这是类型不明确引起的
方法一:
定义类型
r e f s ! : m a i n I n p u t : H T M L I n p u t E l e m e n t t h i s . refs!: { mainInput: HTMLInputElement } this. refs!:mainInput:HTMLInputElementthis.refs.mainInput.focus()
方法二:
首先要去定义mainInput类型,然后再去focus
let mainInputEl: any = this.$refs.mainInput
mainInputEl.focus()

推荐使用方法一

Logo

前往低代码交流专区

更多推荐