template部分
<div v-show="!!pickPictures[0]" class="pickPictures"> <div v-for="(item, index) in pickPictures" :key="index" class="pickItem"> <div class="pickTop" @click="previewImg"> <img class="img" :src="item" /> </div> <div class="pickBtn font28 fontGray" @click="removeTap(index)">删除</div> </div></div><div v-show="pickPictures.length < 4" class="upPic"> <div class="upPicIcon" @click="upImgTap"> <img class="img" src="../assets/images/camera.png" /> </div> <input accept="image/*" type="file" max="1" style="display: none" ref="inputer" @change="changeImg" /></div>
script部分
export default { name: "PictureForm", data() { return { pickPictures: [], }; }, methods: { upImgTap() { let target = this.$refs.inputer; target.click(); }, changeImg() { if (this.pickPictures.length > 5) { Toast("最多上传5张图片!"); return; } let inputDOM = this.$refs.inputer; let files = inputDOM.files; this.uploadImage(files[0]); }, uploadImage(file) { let form = new FormData(); form.append("file", file); config.axios.post(config.upload_async, form).then(res => { console.log(res); if (res.status == 200 && res.data.code == 0) { this.pickPictures.push(res.data.data); } else { Toast("图片上传错误"); return; } }); }, removeTap(index) { let tempList = this.pickPictures; remove(tempList, index); this.pickPictures = tempList; }, getObjectURL(file) { var url = null; if (window.createObjectURL != undefined) { url = window.createObjectURL(file); } else if (window.URL != undefined) { url = window.URL.createObjectURL(file); } else if (window.webkitURL != undefined) { url = window.webkitURL.createObjectURL(file); } return url; }, }};
style部分
.pickPictures { width: 100%; height: 33.3vw; box-sizing: border-box; padding: 0 4.8vw; display: flex; justify-content: flex-start; align-items: center; border-bottom: 1px solid #d1d1d1;}.pickItem { margin-right: 2.67vw; width: 16vw; height: 25.3vw; display: flex; flex-direction: column; justify-content: space-between; align-items: center;}.pickTop { width: 15.7vw; height: 15.7vw; border: 1px solid #e2e2e2; border-radius: 1.3vw; overflow: hidden;}.pickBtn { width: 15.7vw; height: 6vw; border: 1px solid #e2e2e2; border-radius: 0.67vw; line-height: 6vw; text-align: center;}.upPic { width: 100%; height: 25.6vw; box-sizing: border-box; padding: 0 4.8vw; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid #d1d1d1;}.upPicIcon { width: 16vw; height: 16vw;}.upPicTip { width: 70.6vw; line-height: 5.86vw;}