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

自己做网站用软件优帮云排名自动扣费

自己做网站用软件,优帮云排名自动扣费,prizm viewer wordpress,威海高区建设局网站引言 在前端开发中,使用 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://www.dtcms.com/wzjs/235985.html

相关文章:

  • 深圳网站建设美橙互联sem竞价推广公司
  • 美食静态网站设计论文太原做网站的
  • 南京制作网站公司网站谷歌推广怎么做
  • 公司做网站怎么收费站长工具seo推广 站长工具查询
  • 网站建设购物车今天热点新闻事件
  • 安全标准化建设网站网站的推广方案的内容有哪些
  • 地产公司做网站维护写代码么长春网站建设制作
  • 无锡公司网站制作网站的优化
  • 山东德州网站建设哪家便宜流量宝
  • 广州专业建网站百度旗下所有app列表
  • asp技术做网站新手如何涨1000粉
  • 怎样做自己的vip解析网站长沙做网站的公司有哪些
  • 长沙做营销型网站公司seo对网络推广的作用是什么?
  • 中企动力做的网站后台怎么登录速推网
  • 成都哪家做网站好app推广员怎么做
  • 做视频网站资金多少网络营销收获与体会
  • 蚌埠网站建设公司cztv网络营销成功的品牌
  • 网站开发php js国产十大erp软件
  • 华为手表网站如何提高搜索引擎优化
  • 武汉做网站的有哪些免费发广告的平台有哪些
  • 在360网站上怎么做推广海淀区seo搜索引擎优化企业
  • 大连响应式网站建设全球搜索引擎入口
  • 网站怎么换空间商上海培训机构
  • 运城做网站哪家好竞价推广账户托管费用
  • b2b网站推广快速排名点击工具
  • 微信手机网站app制作怎么让百度搜出自己
  • 营销4pseo如何快速排名百度首页
  • 知名网站制作公网络推广是做什么工作的
  • 随州网站建设外包公司视频号怎么推广流量
  • 想学习做网站青岛seo建站