当前位置: 首页 > 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://www.dtcms.com/wzjs/352776.html

相关文章:

  • 东莞网站推广衣裙找网络公司做推广费用
  • 做网站建设需要会哪些网店推广方式
  • 网站防封链接怎么做宣传推广网络推广
  • 小x导航正品seo优化网站排名
  • 深圳精美网站设计个人发布信息免费推广平台
  • 曹鹏 wordpress西安seo优化推广
  • jquery网站合肥百度搜索优化
  • 服装网站建设规划哪些平台可以免费推广
  • 网上写作真正能赚钱的网站发软文的网站
  • 济南网站的建设seo谷歌
  • 深圳最好的网站开发公司电话seo入门培训学校
  • 网站规划与建设课程郑州关键词seo
  • 手机怎么做网站卖东西百度seo视频教程
  • 都有什么推广平台湖南广告优化
  • 高端网站建设jm3q西安外包网络推广
  • 博兴县建设局网站站长之家seo查询官方网站
  • 那些网站能够做推广线下推广方式
  • 南京中天园林建设网站seo工资多少
  • 建一个淘宝客网站需要多少钱百度指数支持数据下载吗
  • wordpress 获取分类列表seo搜索规则
  • 国外设计网站 绿色的app推广接单
  • 日本做a爱片视频网站高级seo优化招聘
  • 珠海建网站wix网站制作
  • 番禺网站制作设计新闻今天最新消息
  • 东莞市天气seo站长
  • 做网站 营业执照平台推广精准客源
  • 佛山网站建设及推广服务公司软文推广发布平台
  • 建设银行公积金查询网站首页南昌seo管理
  • 设计高端网站网页推广平台
  • 做泌尿科网站价格公司专业网站建设