ElementUI pop simple assembly of the package

Vue project, a page may have multiple popups appear, it can be a single bomb box for a simple package, pop hidden by default, when a trigger event or condition, so that the parent component specified pop display.

Window assembly bomb:

 <template>
    <el-dialog :visible.sync="visible" :title="modalObj.title" :width="modalObj.width" :modal='modalObj.modal' :before-close="modalClose" center  :close-on-click-modal="false"
        :close-on-press-escape="false" >
        <el-form :inline='true' :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
            <el-form-item label="任务名称:" prop="taskName">
                <el-input v-model="ruleForm.taskName" placeholder="请输入任务名称" ></el-input>
            </el-form-item>
            <!--弹出层按钮居中-->
            <div style="text-align: center">
                <el-button @click="modalClose" size="small">取消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm',ruleForm)" size="small">保存</el-button>
          </div>
        </el-form>
    </el-dialog>
</template>
<script> 
    export default {
        name: 'timeTask-add-layer',
        data() {
            // 验证
            let validateTaskName = (rule, value, callback) => {
                   let _this = this;
                var pattern = /^.{1,12}$/;
                if (!pattern.test(value)) {
                    callback(new Error('任务名称长度在2到12个字符!'))
                } else {
                    callback();
                }
            }
            return {
                ruleForm: {
                    taskName: ''
                },
                rules: {
                    taskName: [
                        { required: true, message: '请输入任务名称!', trigger: 'blur' },
                        { validator: validateTaskName, trigger: 'change' },
                    ]
                }
            };
        },
        props: {
            visible: {
                type: Boolean,
                default: false
            },
            modalObj: {
                type: Object, 
                default: false
            } 
        },
        mounted() {
            //表单初始
        },
        methods: {
            // 信息提交
            submitForm(formName, formData) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                       //判断modalObj中id是否为空,决定是编辑还是添加请求操作;
                       this.modalClose(); 
                    } else {
                       //验证失败处理
                        return false;
                    }
                });
            },
            // 关闭弹出框
            modalClose() {
                this.$emit('update:visible', false); // 直接修改父组件的属性 
            }
        }
    };
</script>

Parent component pop references:

<template>
  <el-container> 
     <el-button type="primary" icon="el-icon-plus" @click='addTask' size='small'>添加</el-button>
    <!-- 设置定时运行 -->
    <EquiTimingRunModel v-if="equiTimingRunVisible" :visible.sync="equiTimingRunVisible" :modalObj='modalObj'></EquiTimingRunModel>
 
  </el-container>
</template>

<script>
  import EquiTimingRunModel from '@/components/equiTiming-run-layer.vue';
    name: "EquipmentManage",
    components: { 
      EquiTimingRunModel 
    },
    data() {
      return {
        equiTimingRunVisible: false  //弹出框唯一标识
      };
    },
    methods: {  
      // 添加设备信息
      addTask() {
        let _this = this;
        _this.equiTimingRunVisible = true; //指定弹出框显示
        _this.modalObj = { title: '添加',modal: true, width: '700px', id: '' };  //编辑时,id不为空;添加时,id为空。弹窗需要嵌套时,modal: false;不需要则为true。
      }
    }
  };
</script>

 

Published 24 original articles · won praise 3 · views 20000 +

Guess you like

Origin blog.csdn.net/sunny123x/article/details/102599121