vue2 element-ui组件二封-表单组件-按钮封装
这里是一段我们公司过往项目的代码(增删改查项目中的查询/重置按钮)
<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>
看一下这么写的几个弊端(当然代码时没问题的)
- type=primary/type=default 按钮的样式全局调整时非常不便
- size=mini每次都要写, 如果不是复制粘贴的话容易忘
- 查询重置按钮文字, 如查询的平替"检索/搜索", 每个项目要求不同, 全局调整也不方便
- 图标, el-button上有icon属性, 两者写法稍有差异(icon prop中间会有个默认空格, 例如上方没有手动空格的话, 图标和文字是在一起的)
- 图标更换不易, 全局替换可行性也不高(项目大了图标满处都在用)
- type=primary, size=mini这种字符串格式参数, 出了拼写错误很难静态排查
基于上述问题, 有以下几个组件(其实基本上都一样)
效果

用法
文字/图标/样式统一
特殊功能:
- 批量删除click事件触发前会自动二次确认
- import的click事件会带文件(示例代码全局仅支持Excel导入)
- 驳回会自动带审批意见(没封)
<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>
vue2 element-ui组件二封-表单组件-按钮封装的更多相关文章
- vue v-model 与 组件化的表单组件如何沟通
参考mint-ui的代码: https://github.com/ElemeFE/mint-ui/blob/master/packages/radio/src/radio.vue https://gi ...
- (Element UI 组件 Table)去除单元格底部的横线
Element UI 组件 Table 有一个属性 border,添加它可以增加纵向边框,但是无法控制横线边框,因此即使是最简单的 el-table,也会包含一个底部横线. 这个底部横线其实是一个 b ...
- Element Ui使用技巧——Form表单的校验规则rules详细说明
Element UI中对Form表单验证的使用介绍: Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item的 prop 属性设置为需校验的字段名 ...
- element ui组件的开始时间-结束时间验证
<el-date-picker v-model="seach.before" type="date" placeholder="开始时间&quo ...
- 使用iview 的表单组件验证 Upload 组件
使用iview 的表单组件验证 Upload 组件 结果: 点击提交按钮, 没有填的form 项, 提示错误, 当填入数据后提示验证成功 代码: <template> <div id ...
- 文档驱动 —— 表单组件(五):基于Ant Design Vue 的表单控件的demo,再也不需要写代码了。
源码 https://github.com/naturefwvue/nf-vue3-ant 特点 只需要更改meta,既可以切换表单 可以统一修改样式,统一升级,以最小的代价,应对UI的升级.切换,应 ...
- 通过html()的方法获取文本内容, form表单组件显示的值与获取到的值不一致的问题
我在通过 html()获取对应节点的内容,发现一个问题,获取到的 form表单组件的内容值是初始加载的值,而不是经过用户修改后的值.例如页面加载时组件<input type="text ...
- reactjs入门到实战(八)----表单组件的使用
表单组件支持几个受用户交互影响的属性: value,用于 <input>.<textarea> 组件. checked,用于类型为 checkbox 或者 radio 的 &l ...
- 第31讲 UI组件之 Gallery画廊控件
第31讲 UI组件之 Gallery画廊控件 1.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery只 ...
- ReactJS实用技巧(2):从新人大坑——表单组件来看State
不太清楚有多少初学React的同学和博主当时一样,在看完React的生命周期.数据流之后觉得已经上手了,甩开文档啪啪啪的开始敲了起来.结果...居然被一个input标签给教做人了. 故事是这样的:首先 ...
随机推荐
- Redis 源码解读之 Rehash 的调用时机
Redis 源码解读之 Rehash 的调用时机 背景和问题 本文想要解决的问题 什么时机触发 Rehash 操作? 什么时机实际执行 Rehash 函数? 结论 什么时机触发 Rehash 操作? ...
- JZOJ 3281. 【GDOI2013】字母连接
\(\text{Solution}\) 一眼不会,限制有点多... 那就网络流 发下确实是很简单的建图 枚举起点集合 拆点后就很好满足限制了 \(\text{Code}\) #include < ...
- knock:端口敲门服务
knock:端口敲门服务 端口敲门服务,即:knockd服务.该服务通过动态的添加iptables规则来隐藏系统开启的服务,使用自定义的一系列序列号来"敲门",使系统开启需要访问的 ...
- [EULAR文摘] 脊柱放射学持续进展是否显著影响关节功能
脊柱放射学持续进展是否显著影响关节功能 Poddubnyy D, et al. EULAR 2015. Present ID: THU0199. 背景: 强直性脊柱炎(AS)患者的机体功能和脊柱活动性 ...
- redis(13)持久化操作-AOF
AOF(Append Only File) 以日志的形式来记录每个写操作(增量保存),将 Redis 执行过的所有写指令记录下来 (读操作不记录), 只许追加文件但不可以改写文件,redis 启动之初 ...
- []Linux 离线升级Openssh与Openssl版本
https://blog.csdn.net/qq_45728838/article/details/120825845 离线的意思是为Linux无法链接网络,只能通过windows 将文件上传一.首先 ...
- CF1764G1 题解
题意 传送门 交互库有一个 \([1,n]\) 的排列 \(p\),你可以询问 \(l,r,k\),交互库会返回 \(\lfloor\dfrac{p_l}{k}\rfloor,\lfloor\dfra ...
- LG8768 题解
题意 传送门 求长度为 \(n\) 的序列 \(a\) 的个数对 \(998244353\) 取模的结果,其中 \(a\) 满足: \(a_1=w\) \(a_{i-1}+L\le a_i\le a_ ...
- 179. 最大数 (Medium)
问题描述 179. 最大数 (Medium) 给定一组非负整数 nums,重新排列每个数的顺序(每个数不可拆分)使之组成一个最大的整数. 注意: 输出结果可能非常大,所以你需要返回一个字符串而不是整数 ...
- 快速构建用户xlwings环境
一.安装python python-3.8.3-amd64.exe 二.准备文件requirements.txt 内容如下 安装失败需要切换国内镜像源 numpy==1.22.1 openpyxl== ...