子组件
<template>
<div>
<el-dialog
:visible="isShow"
:title="title"
:width="width"
@close="btnCancel"
>
<el-form
:label-width="formConfig.labelWidth"
:inline="formConfig.inline"
:ref="formConfig.ref"
:model="formParams"
>
<el-form-item
:label="item.label"
v-for="(item, index) in formDataList"
:key="index"
:prop="item.prop"
:rules="item.rules"
>
<el-input
v-if="item.type === 'input'"
v-model="formParams[item.prop]"
:placeholder="item.placeholder"
></el-input>
<el-select
v-if="item.type === 'select'"
style="width: 100%"
v-model="formParams[item.prop]"
:placeholder="item.placeholder"
>
<el-option
:label="opItem.label"
:value="opItem.value"
v-for="(opItem, index) in item.options"
:key="index"
></el-option>
</el-select>
<el-date-picker
v-if="item.type === 'date'"
type="date"
style="width: 100%"
v-model="formParams[item.prop]"
:placeholder="item.placeholder"
></el-date-picker>
<el-radio-group
v-if="item.type === 'radio'"
v-model="formParams[item.prop]"
:placeholder="item.placeholder"
>
<el-radio
:label="opItem.label"
v-for="(opItem, index) in item.options"
:key="index"
:rules="item.rules"
></el-radio>
</el-radio-group>
<el-input
v-if="item.type === 'textarea'"
type="textarea"
v-model="formParams[item.prop]"
:placeholder="item.placeholder"
></el-input>
<el-switch
v-if="item.type === 'switch'"
v-model="formParams[item.prop]"
></el-switch>
</el-form-item>
</el-form>
<span slot="footer" class="dialog-footer">
<el-button @click="btnCancel">取 消</el-button>
<el-button type="primary" @click="btnOk">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
props: {
width: {
type: String,
default: "30%",
},
isShow: {
type: Boolean,
default: false,
},
title: {
type: String,
default: "",
},
formDataList: {
type: Array,
default() {
return [];
},
},
formParams: {
type: Object,
default() {
return {};
},
},
formConfig: {
type: Object,
default: () => {
return {};
},
},
},
data() {
return {};
},
methods: {
btnCancel() {
this.$refs[this.formConfig.ref].resetFields();
this.$emit("btnCancel", false);
},
btnOk() {
this.$refs[this.formConfig.ref].validate((valid) => {
if (valid) {
this.$emit("btnOk", this.formParams);
} else {
return false;
}
});
},
},
};
</script>
<style>
</style>
父页面调用子组件
<userFormDialog
:title="title"
:isShow.sync="isShow"
:formDataList="formDataList"
:formParams="formParams"
:formConfig="formConfig"
@btnOk="btnOk"
@btnCancel="btnCancel"
ref="dialogForm"
></userFormDialog>
import userFormDialog from "./userFormDialog.vue";
formParams: {
userName: "",
userLoginName: "",
userPassword: "",
userPhone: "",
userEmali: "",
roleId: "",
},
formDataList: [
{
label: "用户昵称",
prop: "userName",
type: "input",
rules: [
{ required: true, message: "用户昵称不能为空", trigger: "blur" },
],
},
{
label: "登录账户",
prop: "userLoginName",
type: "input",
rules: [
{ required: true, message: "登录账户不能为空", trigger: "blur" },
],
},
{
label: "密码",
prop: "userPassword",
type: "input",
rules: [{ required: true, message: "密码不能为空", trigger: "blur" }],
},
{
label: "用户角色",
prop: "roleId",
type: "select",
options: [
{ label: "管理员", value: 1 },
{ label: "店长", value: 2 },
],
rules: [
{ required: true, message: "用户角色不能为空", trigger: "blur" },
],
},
{
label: "手机号",
prop: "userPhone",
type: "input",
},
{
label: "邮件",
prop: "userEmali",
type: "input",
},
],
formConfig: {
labelWidth: "100px",
inline: false,
ref: "formRef",
},
title: "",
isShow: false,
el-form二次封装后,就只需要先向子组件传入数据(表单formData,定义表单类型的数组)