spt110/vue-typescript-element-ui

typescript element-ui表单验证问题

krast opened this issue · 3 comments

krast commented

ElementUI表单验证问题
this.$refs[formName].validate((valid)
这块怎么用typescript写?不太明白
class Login extends Vue ... this.$refs
这个父组件就是Vue获取不到Form编译就报错了
望指教

krast commented

vuejs/vue-class-component#94

伪代码如下:

(this.$refs[formName] as Form).validate((valid:any) => {
    if (valid) {
     do something
      router.push('/home');
      } else {   
          do something
      }
    });

关键是这句
(this.$refs[formName] as Form)

image
an example to using Form component

This alone is sufficient (i.e. no need to define $refs). Using Vue version 2.6.11.

private async validateForm(): Promise<boolean> {
    try {
      const isValid = await (this.$refs.userForm as any).validate();
      return isValid as boolean;
    } catch (e) {
      return false;
    }
  }