当前位置: 首页 > wzjs >正文

网站开发图比较酷炫的企业网站

网站开发图,比较酷炫的企业网站,建筑知识网站,网站备案是自己可以做吗引言 在前端开发中,使用 Element - UI 组件库来构建用户界面是非常常见的操作。其中图片上传功能更是在许多项目中频繁出现,比如用户头像上传、商品图片上传等场景。有时候,我们会有这样的需求:当上传图片达到一定数量后&#xf…

引言

在前端开发中,使用 Element - UI 组件库来构建用户界面是非常常见的操作。其中图片上传功能更是在许多项目中频繁出现,比如用户头像上传、商品图片上传等场景。有时候,我们会有这样的需求:当上传图片达到一定数量后,隐藏图片上传按钮,避免用户继续上传多余图片。今天,我们就来深入探讨如何实现这一功能。

一、准备工作

(一)项目搭建

首先,确保你的项目已经成功引入了 Element - UI 组件库。如果是基于 Vue.js 的项目,可以通过 npm 或 yarn 进行安装:

npm install element-ui -- save
# 或者
yarn add element-ui

然后在项目的入口文件(如main.js)中进行全局注册:

import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chaldron/index.css';
import App from './App.vue';Vue.use(ElementUI);new Vue({el: '#app',render: h => h(App)
});

(二)基本的图片上传组件使用

在 Vue 组件中,我们可以使用el - upload组件来实现图片上传功能。一个简单的示例如下:

<template><el-uploadclass="upload-demo"action="/your-upload-url":on-change="handleChange":before-upload="beforeUpload":file-list="fileList"list-type="picture - card"><i class="el-icon-plus"></i><div slot="file" slot-scope="{ file }"><img :src="file.url" alt="" class="el-upload-list__item-thumbnail"><span class="el-upload-list__item-actions"><span class="el-upload - list__item-preview" @click="handlePictureCardPreview(file)"><i class="el-icon-zoom-in"></i></span><span class="el-upload-list__item-delete" @click="handleRemove(file)"><i class="el-icon-delete"></i></span></span></div></el-upload>
</template><script>
export default {data() {return {fileList: []};},methods: {handleChange(file, fileList) {console.log(file, fileList);},beforeUpload(file) {return true;},handlePictureCardPreview(file) {// 图片预览逻辑},handleRemove(file, fileList) {// 移除图片逻辑}}
};
</script><style scoped>
.upload - demo {width: 200px;
}
</style>

这里我们设置了图片上传的地址action,定义了上传前的钩子函数beforeUpload,以及图片变化时的钩子函数handleChange等。

二、实现图片上传按钮的隐藏逻辑

(一)设置上传数量限制

我们可以通过el - upload组件的limit属性来设置允许上传的图片数量。例如,我们将其设置为 3,即最多允许上传 3 张图片:

<el-uploadclass="upload-demo"action="/your-upload-url":on-change="handleChange":before-upload="beforeUpload":file-list="fileList"list-type="picture-card":limit="3"
><!-- 其他代码不变 -->
</el-upload>

(二)监听上传图片数量并隐藏按钮

为了实现当上传图片达到一定数量后隐藏上传按钮,我们可以通过计算属性或者watch来监听fileList的长度。

  1. 使用计算属性
<template><el-uploadclass="upload-demo"action="/your-upload-url":on - change="handleChange":before - upload="beforeUpload":file - list="fileList"list - type="picture - card":limit="3":style="{ 'display': canShowUploadButton? 'block' : 'none' }"><i class="el-icon-plus"></i><div slot="file" slot-scope="{ file }"><img :src="file.url" alt="" class="el-upload-list__item-thumbnail"><span class="el-upload-list__item-actions"><span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)"><i class="el-icon-zoom-in"></i></span><span class="el-upload-list__item-delete" @click="handleRemove(file)"><i class="el-icon-delete"></i></span></span></div></el - upload>
</template><script>
export default {data() {return {fileList: []};},computed: {canShowUploadButton() {return this.fileList.length < 3;}},methods: {handleChange(file, fileList) {console.log(file, fileList);},beforeUpload(file) {return true;},handlePictureCardPreview(file) {// 图片预览逻辑},handleRemove(file, fileList) {// 移除图片逻辑}}
};
</script>

在上述代码中,通过计算属性canShowUploadButton来判断fileList的长度是否小于设定的上传数量限制(这里是 3)。如果小于限制数量,则返回true,表示上传按钮可以显示;否则返回false,上传按钮将通过style中的display属性被隐藏。
2. 使用 watch

<template><el-uploadclass="upload-demo"action="/your-upload-url":on-change="handleChange":before-upload="beforeUpload":file-list="fileList"list-type="picture-card":limit="3":style="{ 'display': uploadButtonDisplay }"><i class="el-icon-plus"></i><div slot="file" slot-scope="{ file }"><img :src="file.url" alt="" class="el-upload-list__item-thumbnail"><span class="el-upload-list__item-actions"><span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)"><i class="el-icon-zoom-in"></i></span><span class="el-upload-list__item-delete" @click="handleRemove(file)"><i class="el-icon-delete"></i></span></span></div></el-upload>
</template><script>
export default {data() {return {fileList: [],uploadButtonDisplay: 'block'};},watch: {fileList: {handler(newVal) {if (newVal.length >= 3) {this.uploadButtonDisplay = 'none';} else {this.uploadButtonDisplay = 'block';}},deep: true}},methods: {handleChange(file, fileList) {console.log(file, fileList);},beforeUpload(file) {return true;},handlePictureCardPreview(file) {// 图片预览逻辑},handleRemove(file, fileList) {// 移除图片逻辑}}
};
</script>

这里使用watch来监听fileList的变化,当fileList的长度发生改变时,根据长度是否达到或超过限制数量,动态修改uploadButtonDisplay的值,从而控制上传按钮的显示与隐藏。

三、进一步优化与拓展

(一)提示用户已达上传数量限制

当上传按钮隐藏后,为了给用户更好的提示,我们可以添加一个提示信息,告知用户已经达到上传数量限制。比如,在组件中添加一个el - tooltip

<template><div><el-uploadclass="upload-demo"action="/your-upload-url":on-change="handleChange":before-upload="beforeUpload":file-list="fileList"list-type="picture-card":limit="3":style="{ 'display': canShowUploadButton? 'block' : 'none' }"><i class="el-icon-plus"></i><div slot="file" slot - scope="{ file }"><img :src="file.url" alt="" class="el - upload-list__item-thumbnail"><span class="el-upload-list__item-actions"><span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)"><i class="el-icon-zoom-in"></i></span><span class="el-upload-list__item-delete" @click="handleRemove(file)"><i class="el-icon-delete"></i></span></span></div></el-upload><el-tooltipv-if="!canShowUploadButton"content="已达到上传数量限制"placement="top"><i class="el-icon-information-circle"></i></el-tooltip></div>
</template><script>
export default {data() {return {fileList: []};},computed: {canShowUploadButton() {return this.fileList.length < 3;}},methods: {handleChange(file, fileList) {console.log(file, fileList);},beforeUpload(file) {return true;},handlePictureCardPreview(file) {// 图片预览逻辑},handleRemove(file, fileList) {// 移除图片逻辑}}
};
</script>

(二)动态调整上传数量限制

在实际应用中,上传数量限制可能需要根据不同的业务场景动态调整。我们可以将limit属性设置为一个动态绑定的值,比如从父组件传递过来的 props:

<template><el-uploadclass="upload-demo"action="/your-upload-url":on-change="handleChange":before-upload="beforeUpload":file-list="fileList"list-type="picture-card":limit="uploadLimit":style="{ 'display': canShowUploadButton? 'block' : 'none' }"><!-- 其他代码不变 --></el-upload>
</template><script>
export default {props: {uploadLimit: {type: Number,default: 3}},data() {return {fileList: []};},computed: {canShowUploadButton() {return this.fileList.length < this.uploadLimit;}},methods: {handleChange(file, fileList) {console.log(file, fileList);},beforeUpload(file) {return true;},handlePictureCardPreview(file) {// 图片预览逻辑},handleRemove(file, fileList) {// 移除图片逻辑}}
};
</script>

这样,父组件就可以根据实际需求传递不同的uploadLimit值,灵活调整上传数量限制。

四、总结

通过上述步骤,我们成功实现了在 Element - UI 中,当图片上传达到一定数量后隐藏上传按钮的功能。同时,我们还对其进行了优化和拓展,提升了用户体验和功能的灵活性。在实际项目开发中,我们可以根据具体业务需求进一步完善相关功能,比如添加更丰富的提示信息、优化图片上传的交互流程等。掌握这些技巧,能让我们在使用 Element - UI 构建应用时,打造出更加友好、高效的用户界面。希望本文能对你有所帮助,让你在前端开发的道路上更进一步。


文章转载自:

http://yFpiuAEf.yhyqg.cn
http://2K4AkeTs.yhyqg.cn
http://UJaL8Gbq.yhyqg.cn
http://3svp256U.yhyqg.cn
http://Pt0peBLv.yhyqg.cn
http://6uiv3V6Z.yhyqg.cn
http://UEZkCWF2.yhyqg.cn
http://j7y5CQj3.yhyqg.cn
http://On32Pxxk.yhyqg.cn
http://uSbV1E0E.yhyqg.cn
http://g1awzd1v.yhyqg.cn
http://b7nTuHLJ.yhyqg.cn
http://Z4Wi1NpQ.yhyqg.cn
http://BSI6E7kY.yhyqg.cn
http://hU6GT5nO.yhyqg.cn
http://vjrsEt1c.yhyqg.cn
http://5PGch7fF.yhyqg.cn
http://4qOgbC2y.yhyqg.cn
http://LkIrb3vy.yhyqg.cn
http://fuphao03.yhyqg.cn
http://49gdYvqA.yhyqg.cn
http://F80TXcNg.yhyqg.cn
http://jcOFm4Zt.yhyqg.cn
http://iq8MTtOc.yhyqg.cn
http://xhQfenqL.yhyqg.cn
http://UrlOtak0.yhyqg.cn
http://iYMouZI5.yhyqg.cn
http://q9yt42eX.yhyqg.cn
http://xqJaBo94.yhyqg.cn
http://Iv5Lcjnp.yhyqg.cn
http://www.dtcms.com/wzjs/775777.html

相关文章:

  • 通过页面wordpress文件位置淄博网站制作升级优化
  • 苏州基础网站建设友情链接有什么用
  • 网站开发及维护是什么网站盒子怎么做
  • 南平公司做网站二手房中介网站建设
  • 网站关键词优化建议苏州制作企业网站的
  • 福州seo推广优化贵阳网站关键字优化
  • 购物网站源码下载网站建设会计处理
  • 买卖域名哪个网站好网站地址格式
  • 松岗网站的建设wordpress自适应方法
  • 大公司网站搭建公司wordpress 上传文件 插件
  • 如何用源代码做网站哈尔滨建设局网站首页
  • 深圳微网站搭建wordpress如何把顶部关掉
  • 做搜索引擎网站wordpress七牛图床插件
  • 方圆网通网站建设网上购物流程
  • 广告设计平面设计软件影响seo排名的因素
  • 房产做网站吸引广告文案模板
  • 微网站 底部导航菜单wordpress媒体库图片显示不出来
  • google网站建设wordpress 文章字段
  • 简述网站设计规划的步骤重庆彼客的网站是谁家做的
  • 网站开发与维护的岗位特点职责外国可以做站外推广的网站
  • 彩票做网站wordpress配置网站
  • 品牌网站都有哪些东莞建设网首页
  • 做外贸网站效果城市建设网站
  • 在那些网站上做企业宣传好行业网站 cms
  • 东莞房产网站建设咸阳机场建设招聘信息网站
  • html 学习网站网站制作公司教你怎么制作网站
  • 常州专业网站建设公司成都建设网站哪家好
  • 北京市网站建设公司排名专门拍短视频的公司
  • 北京建筑设计网站Wordpress使用ldap
  • 网站设计与制作合同哈尔滨 高端网站建设