element-ui的dialog弹框的封装

这篇博客详细介绍了如何在Vue项目中封装Element-UI的dialog弹框,包括在`components`目录下创建`dialog-demo.vue`组件,以及在`mixins`目录下编写`index.js`混入方法。在`index.vue`文件中展示了弹框的实际应用,为form-table-demo提供可复用的对话框解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dialog弹框的封装案例

form-table-demo

components

dialog-demo.vue
<template>
  <el-dialog :visible.sync="isShow" v-bind="dialogOption">
    <el-form label-width="100px" :model="form" ref="el_form">
      <el-col :span="24">
        <el-form-item label="用户姓名:" prop="name">
          <el-input clearable placeholder="请输入用户姓名" v-model.trim="form.name"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="手机号:" prop="phone">
          <el-input clearable placeholder="请输入角色名称" v-model.trim="form.phone"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="账号:" prop="account">
          <el-input clearable placeholder="请输入角色名称" v-model.trim="form.account"></el-input>
        </el-form-item>
      </el-col>
    </el-form>
    <div class="footer" style="text-align:center">
      <el-button type="primary" size="medium" class="search-btn">&nbsp;&nbsp;&nbsp;</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { set } from 'lodash'
export default {
  name: 'addUser',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    onSubmit: {
      type: Function,
      default: () => { }
    }
  },
  data() {
    return {
      dialogOption: {
        width: '412px',
        title: '添加用户'
      },
      form: {
        name: '', // 姓名
        phone: '', // 手机号
        account: '' // 账号
      }
    }
  },
  computed: {
    isShow: {
      get() {
        return this.value
      },
      set(v) {
        this.$emit('input', v)
      }
    }
  },
  watch: {
    isShow: {
      handler: 'handleShow',
      immediate: true
    }
  },
  mounted() {
    console.log('dialogOpen');
  },
  methods: {
    handleShow(val) {
      if (val) {
        const { title, width, dialogOption } = this.$attrs
        title && set(this.dialogOption, 'title', title)
        width && set(this.dialogOption, 'width', width)
        dialogOption && Object.assign(this.dialogOption, dialogOption)
        console.log(11)
      } else {
        this.$refs.el_form.clearValidate()
        this.$refs.el_form.resetFields()
      }
    }
  }
}
</script>
<style lang="scss" scoped>
</style>

mixins

index.js
export default {
  data() {
    return {
      dialog: {
        name: '',
        args: null,
        visible: false
      }
    }
  },
  watch: {
    'dialog.visible': function(val) {
      !val && this.$$dialogClose()
    }
  },
  methods: {
    $$dialogOpen(name, args = {}) {
       Object.assign(this.dialog, {
        name,
        args,
        visible: true
      })
    },
    $$dialogClose() {
     const { name } = this.dialog
      this.$refs[name] = null
      Object.assign(this.dialog, {
        name: '',
        args: null
      })
    }
  }
}

index.vue 弹框的使用

<template>
  <div>
    <el-form :model="fromData" ref="ruleForm" label-width="100px" style="padding:30px 0px;">
      <el-row>
        <el-col :span="6">
          <el-form-item label="用户名">
            <el-input clearable placeholder="请输入用户姓名" v-model.trim="fromData.emplName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="员工号">
            <el-input clearable placeholder="请输入员工号" v-model.trim="fromData.psId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="部门">
            <el-input clearable placeholder="请输入部门" v-model.trim="fromData.deptName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" >用户同步</el-button>
            <el-button type="primary" >查询</el-button>
             <!-- 弹框的触发按钮 -->
            <el-button type="primary" @click="handleDialogOpen">打开弹框</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <component :is="dialog.name" :ref="dialog.name" v-model="dialog.visible" v-bind="dialog.args"></component>
  </div>
</template>
<script>
import mixins from './mixins'
import { toString } from 'lodash'
import DialogDemo from './components/dialog-demo'
export default {
  data() {
    return {
      fromData: {
        // 用户名
        emplName: '',
        // 员工号
        psId: '',
        // 部门
        deptName: ''
      }
    }
  },
  components: {
    // 编辑-弹框
    DialogDemo
  },
  mixins: mixins,
  methods: {
       handleDialogOpen() {
       	  // 弹框的打开方法
	      this.$$dialogOpen('DialogDemo', {
	        // title: '你好',
	        // width: '500px'
	        dialogOption: {
	          title: '常增兴你好'
	        },
	        onSubmit: ({ args }) => {
	          console.log('弹框提交', args )
	        }
	      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值