当前位置:   article > 正文

vue2.0 ElementUI父子组件实现子组件表单校验_vue2父组件点保存子组件表单校验

vue2父组件点保存子组件表单校验

vue2.0 ElementUI父子组件实现子组件表单校验

子组件:

< template >
    < div >
    < el - form: model = "form": rules = "rules"
ref = "ruleForm" >
    < el - form - item label = "xxxx"
prop = "fsendtousers" >
    < treeselect v - model = "form.fsendtousers"
style = "width:100%;height:100%;": options = "deptOptions": show - count = "true": multiple = "true"@
select = "selectNode"
placeholder = "请选择xxxx"

/ >
< /el-form-item> < /el-form>
< /div> < /template>
< script >
 import Treeselect from '@riophae/vue-treeselect'
export default {
    name: 'unitTRee',
    components: {
        Treeselect
    },
    props: {
        deptOptions: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        return {
            form: {
                fsendtousers: null
            },
            // 表单验证规则
            rules: {
                // 接收单位
                fsendtousers: [{
                    required: true,
                    message: '请选择xxxxx',
                    trigger: 'change'
                }]
            }
        }
    },
    methods: {
            // 表单验证
            formCheck() {
                return new Promise((resolve, reject) => {
                    this.$refs.ruleForm.validate((valid) => {
                        if (valid) {
                            console.log('submit!')
                            resolve()
                        } else {
                            console.log('error submit!!')
                            reject()
                        }
                    })
                })

            }
    }

} < /script>

< style scoped >
    < /style>
//父组件
      //逻辑代码如下
      //触发子组件的函数实现验证
      this.$refs.unitTRee.formCheck().then(res => {
     //这里做你自己的请求逻辑比如提交功能
      }).catch(err => {
          console.log('验证不通过')
      })
      //子组件应用
      <unitTRee :deptOptions="deptOptions" ref="unitTRee"></unitTRee>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/827197
推荐阅读
相关标签
  

闽ICP备14008679号