el-form 二次封装

子组件

<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,定义表单类型的数组)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值