国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Vue+Vant 圖片上傳加顯示的案例

瀏覽:6日期:2022-11-06 17:47:57

前端開發(fā)想省時(shí)間就是要找框架呀!找框架!

vant中上傳圖片組件:https://youzan.github.io/vant/#/zh-CN/uploader

上傳圖片的組件uploader:

<van-uploader :after-read='onRead' accept='image/*' multiple> <imgclass='head-img' src='http://www.cgvv.com.cn/static/images/addpic.png' ref='goodsImg'/> </van-uploader>

method中

methods: { //選擇圖片后執(zhí)行 onRead(file) { console.log(file); //將原圖片顯示為選擇的圖片 this.$refs.goodsImg.src = file.content; } }

vant上傳的圖片是已經(jīng)base64處理了的,可以直接向后臺(tái)發(fā)了

補(bǔ)充知識(shí):vue +vant + crodova實(shí)現(xiàn)圖片上傳、圖片預(yù)覽、圖片刪除

函數(shù)調(diào)用方法使用圖片預(yù)覽有坑,圖片不顯示

Vue+Vant 圖片上傳加顯示的案例

<template> <div class='add-check-page'> <head-com :title='title'></head-com> <van-form @submit='onSubmit'> <h2 class='van-doc-demo-block__title'>添加照片</h2> <van-field name='uploader' class='pic-uploader'> <template #input> <template v-for='(item, index) in file_path'> <div :key='index'> <van-image fit='cover' :src='http://www.cgvv.com.cn/bcjs/IP + item' @click='preView(index)'><template v-slot:loading> <van-loading type='spinner' size='20' /></template> </van-image> <van-icon name='close' @click='delPic(index)' /> </div> </template> <van-icon @click='picture' :name='require(’#/images/add_check_icon.png’)' /> <van-uploader multiple :after-read='afterRead' style='display:none'> <van-button :icon='require(’#/images/add_check_icon.png’)' type='default' /> </van-uploader> </template> </van-field> <van-button block type='default' native-type='submit'>確認(rèn)提交</van-button> </van-form> <van-action-sheet v-model='show' :actions='actions' @select='onSelect' cancel-text='取消' close-on-click-action /> <loading :showLoading='showLoad'></loading> // 使用函數(shù)調(diào)用圖片預(yù)覽方法,圖片無法顯示所以改用組件調(diào)用 <van-image-preview v-if='imgShow' v-model='imgShow' :images='preList' :start-position='startIndex' @closed='handleClose' ></van-image-preview> </div></template><script>import headCom from ’_c/header/index.vue’import loading from ’_c/loading/index.vue’export default { components: { headCom, loading }, data() { return { // 公司id id: ’’, id2: ’’, title: ’’, file_name: [], file_path: [], content: ’’, show: false, actions: [{ name: ’拍攝’ }, { name: ’從手機(jī)相冊(cè)選擇’ }], showLoad: false, imgPre: ’’, imgShow: false, preList: [], startIndex: 0 } }, beforeRouteLeave(to, from, next) { if (this.imgPre) { this.imgPre.close() } next() }, created() { this.id = this.$route.params.id if (this.$route.name === ’editCheck’) { this.title = ’編輯記錄’ this.getInfo() } else { this.title = ’添加記錄’ } }, methods: { async getInfo() { this.showLoad = true try { let data = { id: this.id } let res = await this.$api.check.edit(data) this.content = res.detail.content this.id2 = res.detail.company_id res.photo_list.forEach((item) => { this.file_name.push(item.file_name) this.file_path.push(item.file_path) }) this.showLoad = false } catch (error) { this.showLoad = false this.$toast(error.msg) } }, async onSubmit(values) { this.showLoad = true try { let data = {} if (this.$route.name === ’editCheck’) { data = { id: this.id, company_id: this.id2, content: values.content, file_names: [...this.file_name], file_paths: [...this.file_path] } await this.$api.check.editPost(data) } else { // 添加 data = { company_id: this.id, content: values.content, file_names: [...this.file_name], file_paths: [...this.file_path] } await this.$api.check.addPost(data) } this.showLoad = false this.$router.go(-1) } catch (error) { this.$toast(error.msg) } }, // 刪除圖片 delPic(index) { this.file_path.splice(index, 1) this.file_name.splice(index, 1) }, // 圖片預(yù)覽 preView(index) { this.startIndex = index this.preList = [] this.file_path.forEach((item) => { this.preList.push(this.IP + item) }) this.imgShow = true }, // 關(guān)閉預(yù)覽 handleClose() { this.preList = [] this.imgShow = false }, async afterRead(file) { this.showLoad = true try { if (file.length) { file.forEach(async (item) => { let res = await this.$api.base.upload(item) this.file_name.push(res.name) this.file_path.push(res.url) this.showLoad = false }) } else { let res = await this.$api.base.upload(file) this.file_name.push(res.name) this.file_path.push(res.url) this.showLoad = false } } catch (e) { this.showLoad = false this.$toast(e.data) } }, picture() { this.show = true }, // 選擇添加圖片方式 onSelect(item) { this.show = false if (item.name === ’拍攝’) { this.takePhoto() } else { let dl = document.getElementById(’photo’) dl.click() } }, // 拍照上傳 takePhoto() { let that = this // crodova 調(diào)取相機(jī)拍照 navigator.camera.getPicture(success, error, {}) function success(imageURI) { that.showLoad = true // file uri 上傳服務(wù)器 that.fileUpload(imageURI) } function error() { this.$toast(’打開相機(jī)失敗’) } }, // 使用cordova FileUpload上傳圖片 fileUpload: function(imageURI) { let that = this let FileUploadOptions = window.FileUploadOptions let FileTransfer = window.FileTransfer let options = new FileUploadOptions() options.fileKey = ’file’ options.fileName = imageURI.substr(imageURI.lastIndexOf(’/’) + 1) options.mimeType = ’image/jpeg’ let ft = new FileTransfer() ft.upload(imageURI, encodeURI(this.API + ’/user/uploadImg’), function(data) { let resString = data.response let resObject = JSON.parse(resString) // 字符串轉(zhuǎn)對(duì)象 if (resObject.code === 1) { that.file_name.push(resObject.data.name) that.file_path.push(resObject.data.url) that.showLoad = false } else { that.showLoad = false that.$toast(resObject.msg) } }) } }}</script>

以上這篇Vue+Vant 圖片上傳加顯示的案例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 亚洲欧美日韩国产vr在线观 | 男操女免费视频 | 国产欧美另类 | 日本一区二区在线 | 99爱在线观看精品视频 | 成年人免费观看视频网站 | 日韩成人毛片高清视频免费看 | 亚洲在线天堂 | 久久视频在线播放视频99re6 | 中文字幕一区二区精品区 | 亚洲免费视频在线观看 | 国产精品短视频免费观看 | 9191精品国产费久久 | 91精品国产综合成人 | 2345成人高清毛片 | 成人做爰 | 日本一视频一区视频二区 | 久久亚洲综合中文字幕 | 国产三级在线视频播放线 | 精品久久久久久乐 | 欧美一级高清片免费一级 | 成人爱做日本视频免费 | 国产一区亚洲欧美成人 | 国产一级片免费观看 | 欧美久草在线 | 日韩高清在线播放不卡 | 中文字幕1区 | 在线观看一级毛片 | 特级无码a级毛片特黄 | 国产在线小视频 | 日韩精品一区二区三区视频网 | 国内高清久久久久久久久 | 豆国产97在线 | 亚洲 | 精品国产自 | 亚洲无吗视频 | 姐姐真漂亮在线视频中文版 | 国产精品国产精品国产三级普 | 国产精品18久久久久久久久久 | 99久久综合国产精品免费 | 性盈盈影院影院67194 | 69日本xxxxxxxxx13|