新增和编辑共用弹窗模板

新增和编辑弹窗模板

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>
相关推荐
前进的李工7 分钟前
智能Agent实战指南:从入门到精通(工具)
开发语言·人工智能·架构·langchain·agent·tool·agentexecutor
小凡同志9 分钟前
从 Vibe Coding 到 Spec-Driven:AI 编程范式的下一次进化
前端·人工智能·架构
hbstream10 分钟前
受够了Vibe Coding的失控?换个起点,让AI事半功倍
前端·人工智能
chxii10 分钟前
在IIS中开启http跳转到https 和 http2的介绍
前端·http·https
小成2023032026518 分钟前
Linux高级03
linux·开发语言
lly20240621 分钟前
Ruby CGI方法详解
开发语言
XiYang-DING22 分钟前
【Java】从源码深入理解HashMap和TreeMap
java·开发语言
霪霖笙箫26 分钟前
「JS全栈AI Agent学习」六、当AI遇到矛盾,该自己决定还是问你?—— Human-in-the-Loop
前端·面试·agent
煜bart28 分钟前
使用 TypeScript 实现算法处理
开发语言·前端·javascript
♛识尔如昼♛29 分钟前
C 基础(7) - 字符输入/输出和输入验证
c语言·开发语言