项目中遇到一个功能,详情页需要循环出上传的附件,但是附件的格式不是唯一,不同格式需要展示不同的图片做区分。
以 Vue 为例:定义一个文件列表,和一个方法,查找文件地址中是否有某一种格式, return 出对应图片的地址。
data: {
fileList: [
{
fileUrl: '/static/file/cbubjbjk.doc',
fileName: '我的简历.doc'
},
{
fileUrl: '/static/file/dvvbnbgfn.docx',
fileName: '2019简历.docx'
},
{
fileUrl: '/static/file/vdbgfn.pdf',
fileName: '最新简历.pdf'
},
]
},
methods:{
getFormat(url) { //判断格式
if(url.search('.docx') !=-1 || url.search('.doc') != -1){
return "../static/img/file-word.png"
}else if(url.search('.pdf') != -1){
return "../static/img/file-pdf.png"
}else{
return "../static/img/file-word.png"
}
},
}
HTML部分:循环出文件列表,并提取出文件中的格式,把图片地址定义给 img 标签。
<div id="app">
<a :href="item.fileUrl" v-for="item in fileList">
<img :src="getFormat(item.fileUrl)" alt="">
<p>{{item.fileName}}</p>
</a>
</div>
这样,就可以根据文件格式,显示不同的图片了。
我把这个小案例放在了我的 GitHub 上面,需要的可以点此访问。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有