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

<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>

最新文章

  1. Webix JavaScript UI 库可以帮你构建跨平台的HTML5 和 CSS3 程序
  2. CentOS7.1配置源
  3. Lambert漫反射.BLinnPhong及Phong模型 Unity自带的在Lighting.cginc里
  4. 《c程序设计语言》读书笔记--大于8 的字符串输出
  5. Performance Counter的使用
  6. Tomcat 设置自动编译,自动发布,自动部署
  7. windows系统port监听
  8. ASP.NET Core 源码学习之 Logging[3]:Logger
  9. leetcode Binary Tree Right Side
  10. 2018年手机应用UI设计趋势预测
  11. 深入理解JVM(五)——垃圾回收器
  12. [ZJOI2008]泡泡堂
  13. 微信小程序中自定义函数的学习使用
  14. 微信小程序之获取用户位置权限(拒绝后提醒)
  15. Jenkins持续集成之小试牛刀
  16. 【基础】selenium中元素定位的常用方法(三)
  17. C#利用反射动态调用DLL并返回结果,和获取程序集的信息
  18. VS C++ 并发编程
  19. float浮点数的四舍五入
  20. Linux+Redis实战教程_day01_Linux介绍与安装

热门文章

  1. U-Boot-基础概念与学习分享
  2. js中Math.floor、Math.ceil、Math.round和parseInt小数取整小结
  3. LeetCode-838 推多米诺
  4. B树,B-树,B+树和B*树
  5. shell 命令小记
  6. Vue 插件介绍
  7. 6. Texture
  8. FTP调优
  9. 一台服务器部署ShareWAF,后面接多台Web服务器,该如何配置?
  10. vue框架2