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

网站专题页怎么做今日重大军事新闻

网站专题页怎么做,今日重大军事新闻,wordpress getfooter,wordpress the_content();引言 在前端开发中,使用 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/83642.html

相关文章:

  • 那些网站做推广怎么免费注册域名
  • 成都建站seo湖北疫情最新情况
  • 江门网站制作网站网站seo推广方案
  • 做网站的材料四平网络推广
  • 网站建设需求问卷电工培训内容
  • 如何给网站做快速排名今日新闻国家大事
  • 做网站下一页昆明seo关键词
  • java做网站的主要技术微信朋友圈广告推广代理
  • 云南网站制作一条龙微博营销案例
  • 公司网站开发费怎么入账产品推广文案范例
  • 做外贸大大小小的网站有哪些体验式营销案例
  • wordpress去掉谷歌字体长沙靠谱关键词优化服务
  • 织梦网站统计国外b站推广网站
  • 游戏网站建设策划方案模板网站seo优化心得
  • 怎么样让客户做网站和小程序推广费用一般多少
  • app定制网站开发深圳市昊客网络科技有限公司
  • 免费建网站的作用百度智能建站平台
  • 住房和城乡建设部网站打不开软文例文 经典软文范例
  • 石家庄市住房和城乡建设厅网站百度免费网站制作
  • 佛山做网站建设价格刷钻业务推广网站
  • 网站敏感字短视频培训机构排名
  • 武汉做网站互助系统成功的网络营销案例ppt
  • 怎样做自己的小说网站公司软文
  • 官网设计需要多少钱seo词条
  • 政府机关网站建设方案如何让新网站被收录
  • php 网站响应时间发表文章的平台有哪些
  • 怎么介绍自己做的网站建立个人网站
  • 佛山微网站建设多少钱西安竞价推广托管
  • 2018做网站前景好么关键词推广软件
  • 没有网站怎么做cpa百度霸屏推广一般多少钱