新增和编辑共用弹窗模板

新增和编辑弹窗模板

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>
相关推荐
牛奔13 分钟前
Go 如何避免频繁抢占?
开发语言·后端·golang
寻星探路4 小时前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
崔庆才丨静觅6 小时前
hCaptcha 验证码图像识别 API 对接教程
前端
lly2024066 小时前
Bootstrap 警告框
开发语言
2601_949146536 小时前
C语言语音通知接口接入教程:如何使用C语言直接调用语音预警API
c语言·开发语言
曹牧6 小时前
Spring Boot:如何测试Java Controller中的POST请求?
java·开发语言
passerby60616 小时前
完成前端时间处理的另一块版图
前端·github·web components
KYGALYX6 小时前
服务异步通信
开发语言·后端·微服务·ruby
掘了7 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
zmzb01037 小时前
C++课后习题训练记录Day98
开发语言·c++