element-ui 二次封装系列- button

elementUI是一个vue.js的ui框架, 在做后台管理系统等方面非常出色,然而面对重复的后台管理系统,大量重复的代码, 这里我们将使用elemnt ui做二次封装,以扩展element ui的属性 来简化代码.

本文以 el-button为例el-button是最简单的组件了,所有操作都需要用到它, 它仅对外提供click事件.
在平时工作中,我们经常遇到一个 重复点击的问题,点太快了接口没有返回会导致多次重复请求,这时我们会想到使用防抖或节流,或者使用el-buttonloading属性, 如果使用loading 属性, 通常 我们会在vuedata中定义一个loading变量,然后调用ajax之前将loading设置为true, ajax返回后将loading设置为false. 这是比较合理的办法,但是如果一个系统非常多这种情况, 那我们需要每一个页面都去定义一个loading变量,作为一个合格的程序员,我们当然想不要写这么多重复的代码,这时候就需要对el-button做二次封装了,
如下代码,我们封装一个pl-button组件

<el-button
  v-bind="$attrs"
  :loading="loadingStatus"
  @click="handleClick"
>
<slot/>
</el-button>
<script>
export default {
    
  name: 'pl-button'
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是一个使用 Element UI 二次封装的 Upload 上传组件的示例代码: ``` <template> <div> <el-upload :action="uploadUrl" :headers="headers" :data="formData" :multiple="multiple" :show-file-list="showFileList" :limit="limit" :on-exceed="onExceed" :before-upload="beforeUpload" :on-success="onSuccess" :on-error="onError" :on-progress="onProgress" > <el-button type="primary" :disabled="uploading">{{ buttonText }}</el-button> <div slot="tip" class="el-upload__tip">{{ tip }}</div> </el-upload> </div> </template> <script> export default { name: "MyUpload", props: { uploadUrl: { type: String, default: "" }, headers: { type: Object, default: () => ({}) }, formData: { type: Object, default: () => ({}) }, multiple: { type: Boolean, default: false }, showFileList: { type: Boolean, default: true }, limit: { type: Number, default: 0 }, buttonText: { type: String, default: "上传文件" }, tip: { type: String, default: "只能上传jpg/png文件,且不超过500kb" } }, data() { return { uploading: false }; }, methods: { onExceed(files, fileList) { this.$message.warning(`只能上传${this.limit}个文件`); }, beforeUpload(file) { const isJPG = file.type === "image/jpeg" || file.type === "image/png"; const isLt500K = file.size / 1024 < 500; if (!isJPG) { this.$message.error("上传图片只能是 JPG/PNG 格式!"); return false; } if (!isLt500K) { this.$message.error("上传图片大小不能超过 500KB!"); return false; } return true; }, onSuccess(response, file, fileList) { this.uploading = false; this.$emit("upload-success", response, file, fileList); }, onError(err, file, fileList) { this.uploading = false; this.$emit("upload-error", err, file, fileList); }, onProgress(event, file, fileList) { this.uploading = true; this.$emit("upload-progress", event, file, fileList); } } }; </script> <style> /* 可以根据自己的需要修改样式 */ .el-upload__tip { font-size: 14px; color: #999; margin-top: 10px; } </style> ``` 这个 Upload 组件支持以下 props: - `uploadUrl`:上传文件的接口地址 - `headers`:上传请求的 headers - `formData`:上传请求的 formData - `multiple`:是否支持多选文件 - `showFileList`:是否显示已上传文件列表 - `limit`:最多上传文件个数 - `buttonText`:上传按钮的文本 - `tip`:上传提示信息 这个 Upload 组件还支持以下事件: - `upload-success`:上传成功的回调函数,参数为 response、file 和 fileList - `upload-error`:上传失败的回调函数,参数为 err、file 和 fileList - `upload-progress`:上传进度的回调函数,参数为 event、file 和 fileList 使用示例: ``` <template> <div> <my-upload :upload-url="uploadUrl" :headers="headers" :form-data="formData" :multiple="multiple" :show-file-list="showFileList" :limit="limit" :button-text="buttonText" :tip="tip" @upload-success="onUploadSuccess" @upload-error="onUploadError" @upload-progress="onUploadProgress" ></my-upload> </div> </template> <script> import MyUpload from "@/components/MyUpload"; export default { components: { MyUpload }, data() { return { uploadUrl: "https://xxx.com/upload", headers: { token: "xxx" }, formData: { type: "avatar" }, multiple: false, showFileList: true, limit: 1, buttonText: "上传头像", tip: "只能上传jpg/png文件,且不超过500kb" }; }, methods: { onUploadSuccess(response, file, fileList) { console.log("上传成功", response, file, fileList); }, onUploadError(err, file, fileList) { console.log("上传失败", err, file, fileList); }, onUploadProgress(event, file, fileList) { console.log("上传进度", event, file, fileList); } } }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值