新增和编辑共用弹窗模板

新增和编辑弹窗模板

html 复制代码
<!-- 添加或编辑用例合集 -->
<template>
    <el-dialog
        :title="`${type == 'add' ? '新建' : type == 'edit' ? '编辑' : ''}`"
        :visible.sync="dialogVisible"
        width="560px"
        :close-on-click-modal="false"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="88px" class="demo-ruleForm">
            <el-form-item label="名称" prop="name"> 
                <el-input v-model="ruleForm.name" placeholder="请输入" :maxlength="30"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="desc">
                <el-input type="textarea" placeholder="请输入" :rows="4" :maxlength="200" v-model="ruleForm.desc"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="handleClose">取 消</el-button>
            <el-button size="small" type="primary" :loading="loading" @click="submitForm('ruleForm')">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "AddOrEditDialog",
    props: {
        rowData: {
            type: Object
        },
        // 弹窗类型
        type: {
            type: String,
        }
    },
    data() {
        return {
            dialogVisible: true,
            ruleForm: {
                name: '',
                desc: '',
            },
            rules: {
                name: [
                    { required: true, message: '请输入合集名称', trigger: 'blur' },
                    { min: 1, max: 30, message: '长度在 1 到 30 个字符', trigger: 'blur' }
                ],
                desc: [
                    { min: 1, max: 200, message: '长度在 1 到 200 个字符', trigger: 'blur' }
                ],
            },
        }
    },
    computed: {
    },
    created() {
        if(this.type == 'edit') {
            this.initForm()
        }
    },
    methods: {
        // 提交表单
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        handleClose() {
            this.$emit('closeDialog', false)
        },
        handleUpdate() {
            this.$emit('update')
        },
        // 初始化表单
        initForm() {
            
        },
    }
}
</script>

<style lang="less" scoped>

</style>

在组件中使用

html 复制代码
<AddOrEditDialog
:type="dialogType"
:rowData="rowData"
@closeDialog="handleClose"
@update="handleUpdate"
></AddOrEditDialog>
相关推荐
碎_浪5 分钟前
给键盘党的英语记忆工具:我做了一款「打字背单词」桌面应用
前端·程序员·ai编程
阿成学长_Cain1 小时前
Linux dirs命令详解|Bash目录堆栈管理快速切换目录实战教程
linux·运维·前端·数据库
多加点辣也没关系1 小时前
JavaScript|第4章:类型转换
开发语言·javascript
聪慧的水蜜桃1 小时前
【YFIOs】用C#开发硬件之设备上云
开发语言·c#
yqcoder1 小时前
httpOnly 是什么,又有什么用?
开发语言·前端·javascript
IT_陈寒2 小时前
Java的Stream.parallel()把我CPU跑爆了,这种优化要谨慎
前端·人工智能·后端
小皮虾2 小时前
小程序首页性能优化实战:从 4 秒到 1.8 秒
前端·微信小程序
烬羽2 小时前
还在手动拼路径、写回调地狱?一文吃透 Node.js 的 path 和 fs
javascript·程序员·node.js
山河木马2 小时前
GPU自动处理专题1-裁剪到底在裁什么(裁剪)
前端·webgl·计算机图形学