• Vee-validate 父组件获取子组件表单校验结果


    vee-validate 是为 Vue.js 量身打造的表单校验框架,允许您校验输入的内容并显示对应的错误提示信息。它内置了很多常见的校验规则,可以组合使用多种校验规则,大部分场景只需要配置就能实现开箱即用,还支持自定义正则表达式。而且支持 40 多种语言,对本地化、多语言支持非常友好。

    国内饿了么团队开源项目 Element UI 就用到了 vee-validate

    vee-validate官网:https://baianat.github.io/vee-validate/

    使用方法

    可查看官网文档(https://baianat.github.io/vee-validate/)或者查看这一篇文章(https://blog.givebest.cn/javascript/2019/04/20/vue.js-nuxt.js-use-vee-validate.html)。

    组件内使用 Vee-validate

    子组件

    <template>
      <div>
        <input
          placeholder="请输入姓名"
          v-model="username"
          name="username"
          v-validate="'required'"
          :error-message="errors.first('username')"
        />
      </div>
    </template>
    
    <script>
    export default {
      name: "Username",
      data() {
        return {
          username: ''
        }
      },
      methods: {
        // 表单校验
        validateForm() {
          return this.$validator.validateAll();
        },
      }
    };
    </script>
    

    父组件

    <template>
      <div>
        <Username ref="usernameComponent" />
        <Password ref="passwordComponent" />
    
        <div>
          <button @click="onSubmit">提交校验</button>
        </div>
      </div>
    </template>
    
    <script>
    import Username from "~/components/username.vue"
    import Password from "~/components/password.vue"
    
    export default {
      components: {
        Username,
        Password
      },
      data() {
        return {}
      },
      methods: {
        onSubmit (e) {
          e.preventDefault()  // 阻止默认事件
    
          // 父组件触发子组件校验,并通过 Promise 返回值
          let vf1 = this.$refs.usernameComponent.validateForm()
          let vf2 = this.$refs.passwordComponent.validateForm()
    
          // 提交表单前,校验所有子组件,全部通过才允许下面操作
          Promise.all([vf1, vf2]).then(result => {
            // 有一个组件未通过,就提示错误信息
            if (result.indexOf(false) > -1) {
              console.log("全部校验未通过")
              return
            }
    
            // 校验全部通过处理
            console.log("全部校验通过")
          })
        },
      }
    };
    </script>
    

    总结

    其实组件内使用 Vee-validate 校验很方便,主要问题可能是父组件怎么触发子组件内的校验,并获取校验结果。这里用到 Vue.js 里的 ref 特性,给子组件赋值一个 ID 引用,然后就可以使用 this.$refs.childComponent 获得子组件实例引用,再分别调起子组件写好的校验方法,如:

    /**
    父组件触发子组件校验,并通过 Promise 返回值
    */
    let vf1 = this.$refs.usernameComponent.validateForm() // 父组件调用 usernameComponent 组件里的 validateForm 方法
    let vf2 = this.$refs.passwordComponent.validateForm() // 父组件调用 passwordComponent 组件里的 validateForm 方法
    

    然后通过 Promise.all 获取全部子组件校验结果后,再根据结果来判断,是否全部通过,分别做出不同处理。

    // 提交表单前,校验所有子组件,全部通过才允许下面操作
    Promise.all([vf1, vf2]).then(result => {
      // 有一个组件未通过,就提示错误信息
      if (result.indexOf(false) > -1) {
        console.log("全部校验未通过")
        return
      }
    
      // 校验全部通过处理
      console.log("全部校验通过")
    })
    

    转载请注明出处: https://blog.givebest.cn/javascript/2019/05/18/vee-validate-get-subcomponent-verification-results.html

  • 相关阅读:
    scrapy(二)内容获取
    scrapy(一)建立一个scrapy项目
    scrapy(四)使用redis
    scrapy(三)使用mongoDB
    索引处的解码字符串
    Golang竞争状态
    Golang之泛型编程-细节
    区块链学这个就够了-DLT(一)
    Linux日志分析-Ubuntu(一)
    经典博弈-int
  • 原文地址:https://www.cnblogs.com/givebest/p/10885099.html
Copyright © 2020-2023  润新知