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

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

vue2 element-ui组件二封-表单组件-按钮封装的更多相关文章

  1. vue v-model 与 组件化的表单组件如何沟通

    参考mint-ui的代码: https://github.com/ElemeFE/mint-ui/blob/master/packages/radio/src/radio.vue https://gi ...

  2. (Element UI 组件 Table)去除单元格底部的横线

    Element UI 组件 Table 有一个属性 border,添加它可以增加纵向边框,但是无法控制横线边框,因此即使是最简单的 el-table,也会包含一个底部横线. 这个底部横线其实是一个 b ...

  3. Element Ui使用技巧——Form表单的校验规则rules详细说明

    Element UI中对Form表单验证的使用介绍: Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item的 prop 属性设置为需校验的字段名 ...

  4. element ui组件的开始时间-结束时间验证

    <el-date-picker v-model="seach.before" type="date" placeholder="开始时间&quo ...

  5. 使用iview 的表单组件验证 Upload 组件

    使用iview 的表单组件验证 Upload 组件 结果: 点击提交按钮, 没有填的form 项, 提示错误, 当填入数据后提示验证成功 代码: <template> <div id ...

  6. 文档驱动 —— 表单组件(五):基于Ant Design Vue 的表单控件的demo,再也不需要写代码了。

    源码 https://github.com/naturefwvue/nf-vue3-ant 特点 只需要更改meta,既可以切换表单 可以统一修改样式,统一升级,以最小的代价,应对UI的升级.切换,应 ...

  7. 通过html()的方法获取文本内容, form表单组件显示的值与获取到的值不一致的问题

    我在通过 html()获取对应节点的内容,发现一个问题,获取到的 form表单组件的内容值是初始加载的值,而不是经过用户修改后的值.例如页面加载时组件<input type="text ...

  8. reactjs入门到实战(八)----表单组件的使用

    表单组件支持几个受用户交互影响的属性: value,用于 <input>.<textarea> 组件. checked,用于类型为 checkbox 或者 radio 的 &l ...

  9. 第31讲 UI组件之 Gallery画廊控件

    第31讲 UI组件之 Gallery画廊控件 1.Gallery的简介 Gallery(画廊)是一个锁定中心条目并且拥有水平滚动列表的视图,一般用来浏览图片,并且可以响应事件显示信息.Gallery只 ...

  10. ReactJS实用技巧(2):从新人大坑——表单组件来看State

    不太清楚有多少初学React的同学和博主当时一样,在看完React的生命周期.数据流之后觉得已经上手了,甩开文档啪啪啪的开始敲了起来.结果...居然被一个input标签给教做人了. 故事是这样的:首先 ...

随机推荐

  1. Spring Boot Hello World 基于 IDEA 案例详解

    一.Spring Boot 是什么 世界上最好的文档来源自官方的<Spring Boot Reference Guide>,是这样介绍的: Spring Boot makes it eas ...

  2. c#反射优化

    https://www.cnblogs.com/xinaixia/p/5777886.html https://www.cnblogs.com/xinaixia/p/5777961.html

  3. 第五周作业-N67044-张铭扬

    1.  搭建chrony服务完成多个主机的时间同步. [root@centos8 ~]# yum -y install chrony [root@centos8 ~]# vim /etc/chrony ...

  4. Day 24 24.2:逆向分析2 - 完美世界案例

    完美世界逆向分析 url:https://passport.wanmei.com/login?location=L3NhZmUv 定位到正确的断点位置 进行js改写操作 断点代码的关键字:setPub ...

  5. 实践:腾讯云IM搭建应用内类微信社交聊天模块

    社交模块是目前主流应用程序最常见的功能之一.有了社交模块,用户在您的应用内,可以自由的交流互动,并添加好友,关注其他用户等等.这可在很大程度上,促进您应用程序的活跃度,吸引用户留存,获取更多新用户,并 ...

  6. 字典集合:Dictionary

    字典键值对的键是唯一的,如果添加了相同键的项就会抛异常,可以通过索引的方式进行重新赋值 using System; using System.Collections.Generic; using Sy ...

  7. flask相关

    app使用flask_session pip install flask_session from flask_session import Session app = Flask(__name__) ...

  8. 使用elemeng-plus控制台报错:$weight

    使用element-plus最开始按需使用,加入的组件少没有问题,但组件引入多了以后发现控制台会报下面的警告: Deprecation Warning: $weight: Passing a numb ...

  9. 硬件IIC的7位从机地址查询方式读写参考代码

    目录 本文中使用582测试,在整合先前博客中的代码的基础上,加上读写超时,加上返回值,加上16位从机寄存器地址的判断,希望读写各用一个函数就能解决硬件IIC的使用问题. #include " ...

  10. Mybaties中的报错 Tag name expected解决

    有些时候一些小小的报错可能会没有注意到,等到报错的时候才发现,一个小小的细节也是很重要的,毕竟我们是bug生产员 来看报错的代码 when round((UNIX_TIMESTAMP(DATE_ADD ...