最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年11月03日 09:08:16   作者:HYeeee  
這篇文章主要介紹了Vue+Vant 圖片上傳加顯示的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

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

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

上傳圖片的組件uploader:

 <van-uploader :after-read="onRead" accept="image/*" multiple>
   <imgclass="head-img" src="/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ù)覽有坑,圖片不顯示

<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 class="item" :key="index">
       <van-image fit="cover" :src="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 class="up-btn" @click="picture" :name="require('#/images/add_check_icon.png')" />
     <van-uploader id="photo" multiple :after-read="afterRead" style="display:none">
      <van-button
       class="up-btn"
       :icon="require('#/images/add_check_icon.png')"
       type="default"
      />
     </van-uploader>
    </template>
   </van-field>
   <van-button class="save" 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ī)相冊選擇' }],
   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 圖片上傳加顯示的案例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue與react詳細(xì)

    vue與react詳細(xì)

    react在中后臺(tái)項(xiàng)目中由于在處理復(fù)雜的業(yè)務(wù)邏輯或組件的復(fù)用問題比vue優(yōu)雅而被人認(rèn)可,但也更需要團(tuán)隊(duì)技術(shù)整體比較給力,領(lǐng)頭大佬的設(shè)計(jì)與把關(guān)能力要更優(yōu)秀,因此開發(fā)成本更大,下面文章就來詳細(xì)介紹,需要的朋友可以參考下
    2021-09-09
  • Vue toRef toRefs toRaw函數(shù)使用示例

    Vue toRef toRefs toRaw函數(shù)使用示例

    這篇文章主要介紹了Vue toRef toRefs toRaw函數(shù)使用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2023-03-03
  • Vue使用Tinymce富文本自定義toolbar按鈕的實(shí)踐

    Vue使用Tinymce富文本自定義toolbar按鈕的實(shí)踐

    本文主要介紹了Vue使用Tinymce富文本自定義toolbar按鈕,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 基于Vue2實(shí)現(xiàn)移動(dòng)端圖片上傳、壓縮、拖拽排序、拖拽刪除功能

    基于Vue2實(shí)現(xiàn)移動(dòng)端圖片上傳、壓縮、拖拽排序、拖拽刪除功能

    這篇文章主要介紹了基于Vue2實(shí)現(xiàn)移動(dòng)端圖片上傳、壓縮、拖拽排序、拖拽刪除功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • vue-swiper的使用教程

    vue-swiper的使用教程

    swiper是我之前做前端頁面會(huì)用到的一個(gè)插件,我自己認(rèn)為是非常好用的。這篇文章給大家介紹vue-swiper的使用教程,感興趣的朋友一起看看吧
    2018-08-08
  • vue router 通過路由來實(shí)現(xiàn)切換頭部標(biāo)題功能

    vue router 通過路由來實(shí)現(xiàn)切換頭部標(biāo)題功能

    在做單頁面應(yīng)用程序時(shí),一般頁面布局頭尾兩塊都是固定在布局頁面,中間為是路由入口。這篇文章主要介紹了vue-router 通過路由來實(shí)現(xiàn)切換頭部標(biāo)題 ,需要的朋友可以參考下
    2019-04-04
  • vue執(zhí)行配置選項(xiàng)npm?run?serve的本質(zhì)圖文詳解

    vue執(zhí)行配置選項(xiàng)npm?run?serve的本質(zhì)圖文詳解

    本地開發(fā)一般通過執(zhí)行npm run serve命令來啟動(dòng)項(xiàng)目,那這行命令到底存在什么魔法?下面這篇文章主要給大家介紹了關(guān)于vue執(zhí)行配置選項(xiàng)npm?run?serve的本質(zhì)的相關(guān)資料,需要的朋友可以參考下
    2023-05-05
  • Vue中ref、reactive、toRef、toRefs、$refs的基本用法總結(jié)

    Vue中ref、reactive、toRef、toRefs、$refs的基本用法總結(jié)

    這篇文章主要給大家介紹了關(guān)于Vue中ref、reactive、toRef、toRefs、$refs的基本用法,以及他們之家的區(qū)別,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-11-11
  • Vuex的五大核心詳細(xì)講解

    Vuex的五大核心詳細(xì)講解

    這篇文章主要為大家介紹了vuex的五個(gè)核心概念和基本使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-09-09
  • Vuex模塊化與持久化深入講解

    Vuex模塊化與持久化深入講解

    在實(shí)際項(xiàng)目開發(fā)過程中,如果公共數(shù)據(jù)比較多我們會(huì)使用vuex做公共狀態(tài)管理,但是在對(duì)瀏覽器進(jìn)行刷新操作的時(shí)候,會(huì)導(dǎo)致vuex內(nèi)的數(shù)據(jù)丟失,這種情況有些時(shí)候是沒問題的,但是有的時(shí)候我們需要某些數(shù)據(jù)可以持久化的保存,這樣就需要做對(duì)應(yīng)的處理
    2023-01-01

最新評(píng)論

屯留县| 公主岭市| 东丽区| 黎城县| 巴中市| 曲靖市| 铜梁县| 尚义县| 衡山县| 甘肃省| 邻水| 河间市| 利川市| 牟定县| 清水河县| 确山县| 进贤县| 永福县| 南川市| 昌江| 青神县| 雷山县| 镇原县| 钦州市| 襄樊市| 会同县| 中牟县| 勃利县| 红桥区| 昌江| 突泉县| 闻喜县| 扎囊县| 浙江省| 博罗县| 齐河县| 晋城| 云阳县| 洱源县| 繁峙县| 维西|