首页 > 其他分享 >vue2 element-ui组件二封-表单组件-按钮封装

vue2 element-ui组件二封-表单组件-按钮封装

时间:2023-01-09 11:24:23浏览次数:43  
标签:二封 default FormButtonMixin mixins export ui 组件 import button

这里是一段我们公司过往项目的代码(增删改查项目中的查询/重置按钮)

<el-button @click="query()" type="primary" size="mini">
  <i class="el-icon-search">查询</i>
</el-button>
<el-button @click="resetQueryForm" type="default" size="mini">
  <i class="el-icon-refresh-left">重置</i>
</el-button>

看一下这么写的几个弊端(当然代码时没问题的)

  1. type=primary/type=default 按钮的样式全局调整时非常不便
  2. size=mini每次都要写, 如果不是复制粘贴的话容易忘
  3. 查询重置按钮文字, 如查询的平替"检索/搜索", 每个项目要求不同, 全局调整也不方便
  4. 图标, el-button上有icon属性, 两者写法稍有差异(icon prop中间会有个默认空格, 例如上方没有手动空格的话, 图标和文字是在一起的)
  5. 图标更换不易, 全局替换可行性也不高(项目大了图标满处都在用)
  6. type=primary, size=mini这种字符串格式参数, 出了拼写错误很难静态排查

基于上述问题, 有以下几个组件(其实基本上都一样)

效果

用法

文字/图标/样式统一

特殊功能:

  1. 批量删除click事件触发前会自动二次确认
  2. import的click事件会带文件(示例代码全局仅支持Excel导入)
  3. 驳回会自动带审批意见(没封)
<ly-btn-search @click="todo"/>
<ly-btn-reset @click="todo"/>
<ly-btn-create @click="todo"/>
<ly-btn-remove @click="todo"/>
<ly-btn-export @click="todo"/>
<ly-btn-import @click="todo"/>

代码

mixin

所有按钮支持禁用和loading状态(虽然大部分按钮用不到)

export const FormButtonMixin = {
  props: {
    /**
     * 禁用状态
     */
    disabled: {
      type: [Boolean, Object, String, Number]
    },
    /**
     * 加载状态
     */
    loading: Boolean
  }
}

LyBtnSearch 检索

<template>
  <el-button
    :disabled="disabled"
    :loading="loading"
    icon="el-icon-search"
    size="small"
    type="primary"
    @click="$emit('click')"
  >
    <slot>检索</slot>
  </el-button>
</template>

<script>
import { FormButtonMixin } from './mixins/form-button-mixin'

export default {
  name: 'LyBtnSearch',
  mixins: [FormButtonMixin]
}
</script>

LyBtnReset 重置

<template>
  <el-button
    icon="el-icon-refresh-left"
    size="small"
    type="default"
    @click="$emit('click')"
  >
    <slot>重置</slot>
  </el-button>
</template>

<script>
import { FormButtonMixin } from './mixins/form-button-mixin'

export default {
  name: 'LyBtnReset',
  mixins: [FormButtonMixin]
}
</script>

LyBtnCreate 新增

<template>
  <el-button
    class="ly-form-button"
    :loading="loading"
    :disabled="disabled"
    icon="el-icon-plus"
    type="primary"
    size="small"
    @click="$emit('click')"
  >
    <slot>添加</slot>
  </el-button>
</template>

<script>

import { FormButtonMixin } from './mixins/form-button-mixin'

export default {
  name: 'LyBtnCreate',
  mixins: [FormButtonMixin]
}
</script>

LyBtnRemove (批量)删除

<template>
  <el-button
    class="ly-form-button"
    :loading="loading"
    :disabled="disabled"
    icon="el-icon-delete"
    type="danger"
    size="small"
    @click="handleClick"
  >
    <slot>批量删除</slot>
  </el-button>
</template>

<script>

import { FormButtonMixin } from './mixins/form-button-mixin'

export default {
  name: 'LyBtnRemove',
  mixins: [FormButtonMixin],
  props: {
    /**
     * 为true时, 删除操作不需要二次确认
     */
    unimportant: Boolean,
    /**
     * 提示内容示例:
     * ())=>`确认要删除${xxx}吗`
     */
    tip: {
      type: [String, Function],
      default: '确认删除选中内容吗?'
    }
  },
  methods: {
    handleClick() {
      if (this.unimportant) {
        return this.$emit('click')
      }
      let tip = this.tip
      if (typeof tip === 'function') {
        tip = tip()
      }
      this.$confirm(tip, '提示', { type: 'warning' }).then(_ => this.$emit('click'))
    }
  }
}
</script>

LyBtnImport 导入

<template>
  <el-button
    :disabled="disabled"
    :loading="loading"
    icon="el-icon-upload2"
    size="small"
    type="primary"
    @click="handleClick"
  >
    <input
      ref="fileRef"
      style="display: none"
      type="file"
      accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      @change="handleFileChange"
    >
    <slot>导入</slot>
  </el-button>
</template>

<script>
import { FormButtonMixin } from './mixins/form-button-mixin'

export default {
  name: 'LyBtnImport',
  mixins: [FormButtonMixin],
  methods: {
    /**
     * 文件变更
     * @param {Event} e
     */
    handleFileChange(e) {
      /**
       * @type {HTMLInputElement}
       */
      const target = e.target
      const file = target.files[target.files.length - 1]
      this.$emit('change', file)
    },
    handleClick() {
      this.$refs.fileRef.dispatchEvent(new MouseEvent('click'))
    }
  }
}
</script>

LyBtnExport 导出

<template>
  <el-button
    :disabled="disabled"
    :loading="loading"
    icon="el-icon-download"
    size="small"
    type="primary"
    @click="$emit('click')"
  >
    <slot>导出</slot>
  </el-button>
</template>

<script>
import { FormButtonMixin } from './mixins/form-button-mixin'

export default {
  name: 'LyBtnExport',
  mixins: [FormButtonMixin]
}
</script>

标签:二封,default,FormButtonMixin,mixins,export,ui,组件,import,button
From: https://www.cnblogs.com/liangyun/p/17036457.html

相关文章

  • vue2组件props;computed监控变量,watch执行方法
    props:{mesData:{type:Object,//接受父组件值required:true,},tableLod:{type:Function,......
  • swagger-ui的使用
    引入依赖<!--接口文档依赖--><dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</version></depe......
  • layui Table控件选择一行获取选择行信息
    layuiTable控件选择一行获取选择行信息1、获取Table操作句柄//临时保存JSONvarcoursetime;//table控制变量vartablecourse;functioninitDataTable(dt){c......
  • 界面组件DevExpress WinForms v22.2最新版本系统环境配置要求
    DevExpressWinForms拥有180+组件和UI库,能为WindowsForms平台创建具有影响力的业务解决方案。DevExpressWinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office......
  • ExtJS-UI组件-Segmented Button
    更新记录详细更新记录```2023年1月6日从笔记迁移到博客。```转载请注明出处:https://www.cnblogs.com/cqpanda/p/17031386.htmlExtJS教程汇总:https://www.cnblo......
  • RabbitMQ GUI客户端工具
    RabbitMQGUI客户端工具(RabbitMQAssistant)平时用控制台或者网页进行管理不免有点不方便,尤其在读取消息的时候不支持过滤和批量发送消息,在此推荐一个漂亮的GUI客户端工具......
  • 14.ElementUI
    ElementUI视频教程(P149~P153)Element:是饿了么公司前端开发团队提供的一套基于Vue的网站组件库,用于快速构建网页组件:组成网页的部件,例如超链接、按钮、图片、表格等......
  • Higress & Kruise Rollout: 渐进式交付为应用发布保驾护航
    作者:扬少前言在业务高速发展过程中,如何最大化保障功能迭代过程中业务流量无损一直是开发者比较关心的问题。通常在应用发布新功能阶段,我们会采用灰度发布的思想对新版本......
  • UIAutomation.0.8.7B3.samples uia powershell 插件例子解析
     uiautomationpowershell插件例子解析  作者给出了示例,不过在中文版Windows上需要略微修改下。因为中文版的进程名名字跟程序名字可能不一样。作者给出里例子是按首......
  • DNUICTF
    Web[签到]flag等一会后把内容复制下来,然后执行一下脚本:importreimportbase64flag=''f=open('flag.txt','r')content=f.read()foriinrange(20):......