element ui Dialog 对话框关闭后——清除表单的效验方法(更简便写法)

效果展示:

主要代码:在关闭弹窗时 有个组件的关闭的回调 @close='cancel' 调用这 cancel方法就可以实现 关闭的同时清除表单效验

cancel() { //关闭弹窗 的同时 清除表效验内容
     this.clone()
     this.reset('ruleForm') // ruleForm是 :model表单绑定的
   },
 clone() {  //关闭弹窗
      this.dialogVisible = false
  },
  reset(formName) {//清除表效验内容
      this.from={}
      this.$refs[formName].resetFields();
 },

完整代码展示

父组件:

<template> 
          // isreject为true时打开弹窗 false则关闭
    <reject :open.sync="isreject"/>
    <el-button @click='btn'>点击触发弹窗</el-button>
</template>
<script>
   import reject from '../reject.vue';//引入文件
   export default {
   components: {
        reject //注册该组件
    },
   data:{
      isreject:false
   },
  methods:{
    btn(){
       this.isreject = true
    }       
  }
 }
</script>

子组件:

<template>
    <el-dialog title="驳回"  append-to-body  :visible="open" width="30%" @close="cancel">
        <el-form :model="ruleForm" ref="ruleForm" label-width="100px">
            <el-form-item label="驳回原因" >
                <el-input :rows='4' type="textarea" v-model="ruleForm.desc"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="cancel">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            ruleForm: {},
        }
    },
    props: {//从父元素传递过来的参数
        open: Boolean,
    },
    methods: {
     cancel() { //关闭弹窗 的同时 清除表效验内容
         this.clone()
         this.reset('ruleForm')
     },
     clone() {关闭弹窗
          this.$emit('update:open', false);
     },
     reset(formName) {//清除表效验内容
         this.ruleForm={}
         this.$refs[formName].resetFields();
     },
   }
}
</script>

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值