vue typescript element

在做表单验证,下面是代码

1
2
3
4
5
6
7
8
9
submitForm (formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!')
} else {
console.log('error submit!!')
return false
}
})

结果出现报错

1
2
TS2339: Property 'validate' does not exist on type 'Vue | Element | Vue[] | Element[]'.
Property 'validate' does not exist on type 'Vue'.

造成这个原因是因为没有定义类型,这样改写就可以

1
2
3
4
5
6
7
8
9
10
11
submitForm (formName) {
let el:any = this.$refs[formName]
el.validate((valid) => {
if (valid) {
alert('submit!')
} else {
console.log('error submit!!')
return false
}
})
}

这样就可以了。