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

element中的el-upload附件上傳與附件回顯

 更新時(shí)間:2022年08月01日 11:05:42   作者:左手牽??右手  
這篇文章主要介紹了element中的el-upload附件上傳與附件回顯方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

開發(fā)中經(jīng)常也會(huì)遇到附件的上傳和回顯,最方便的就是我們封裝一個(gè)公共組件在頁(yè)面中引用

1、上傳

在src里面新建一個(gè)文件夾

<template>
  <el-upload class="upload-demo"
             multiple
             :limit="limit"
             :accept="accept"
             :headers="headers"
             :file-list="fileList"
             :action="uploadImgUrl"
             :on-exceed="handleExceed"
             :on-remove="handleRemove"
             :on-success="handleSuccess"
             :before-upload="beforeUpload"
             :on-error="handleUploadError">
    <!-- :on-preview="handlePreview" -->
    <el-button size="small"
               type="primary">點(diǎn)擊上傳</el-button>
    <div slot="tip"
         class="el-upload__tip"
         style="color:#909399">
      支持上傳{{ accept === "*" ? "所有" : accept }}文件,且不超過20MB,附件名稱不能超過50字
    </div>
  </el-upload>
</template>
<script>
import { getToken } from '@/utils/auth'
export default {
  components: {},
  props: {
    value: {
      type: String,
      default: null
    },
    accept: {
      type: String,
      default: '*'
    },
    limit: {
      type: Number,
      default: 1
    }
  },
  data () {
    return {
      uploadImgUrl: process.env.VUE_APP_BASE_API + '/common/upload', // 上傳的圖片服務(wù)器地址
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
      fileList: [],
      returnData: []
    }
  },
  watch: {},
  mounted () {
    this.value === null && this.value === '' ? (this.fileList = []) : this.getFileList()
  },
  methods: {
    getFileList () {
      if (this.value !== null && this.value !== '') {
        const accessory = JSON.parse(this.value)
        this.fileList = []
        accessory.map(val => {
          this.fileList.push({
            name: val.name,
            // 編輯修改
            // response: {
            //   fileName: val.url
            // },
            response: {
              data: {
                fileName: val.url
              }
            }         
          })
        })
      }
    },
    // 刪除上傳文件后
    handleRemove (file, fileList) {
      this.getReturnData(fileList)
    },
    // 上傳前------------文件大小和格式校驗(yàn)
    beforeUpload (file) {
      if (this.accept !== '*') {
        var fileType = file.name.substring(file.name.lastIndexOf('.') + 1)
        const accept = this.accept.split(',')
        if (accept.indexOf('.' + fileType) < 0) {
          this.$message.warning('請(qǐng)選擇正確的文件格式')
          return false
        }
      }
      const isLt50M = file.size / 1024 / 1024 < 20
      if (!isLt50M) {
        this.$message.warning('上傳附件大小不能超過 20MB!')
        return false
      }
      return true
    },
    // 附件上傳成功后的鉤子
    handleSuccess (response, file, fileList) {
      if (response.code === 200) {
        this.getReturnData(fileList)
      } else {
        this.$message.error(response.msg)
        this.fileList=[]
      }
    },
    handleUploadError () {
      this.$message({
        type: 'error',
        message: '上傳失敗'
      })
    },
    // 獲取附件信息后進(jìn)行返回處理
    getReturnData (fileList) {
      this.returnData = []
      console.log(fileList)
      fileList.map(val => {
        this.returnData.push({
          name: val.name,
          url: val.response.data.fileName
        })
      })
      this.$emit('input', JSON.stringify(this.returnData))
    },
    // 點(diǎn)擊上傳文件的鉤子
    handlePreview (file) {
      var a = document.createElement('a', '_bank')
      var event = new MouseEvent('click')
      a.download = file.name
      a.href = file.response.url
      a.dispatchEvent(event)
    },
    // 文件超出個(gè)數(shù)限制時(shí)的鉤子
    handleExceed (files, fileList) {
      this.$message.warning(`當(dāng)前限制只能上傳 ` + this.limit + ` 個(gè)文件`)
    },
    // 刪除文件之前的鉤子,參數(shù)為上傳的文件和文件列表,若返回 false 或者返回 Promise 且被 reject,則停止刪除。
    beforeRemove (file, fileList) {
      return this.$confirm(`確定移除 ${file.name}?`)
    }
  }
}
</script>

代碼中這個(gè)import { getToken } from ‘@/utils/auth’,其實(shí)是獲取的存在Cookies里面的token的值,大家可以根據(jù)自己項(xiàng)目情況進(jìn)行修改使用

在頁(yè)面中直接引入使用就可以了:

2、附件回顯

和上傳一樣同樣新建一個(gè)文件夾

<template>
  <div class="upload-detail">
    <div class="detail"
         style="line-height:20px"
         v-if="accessory === '[]' || accessory === null">無</div>
    <div v-else>
      <template v-for="(val,key) in upload.fileList">
        <el-link :key="key"
                 class="detail"
                 :underline="false"
                 @click="handlePreview(val)">{{val.name}}
          <span class="icon-emad- icon-emad-xiazai"> 下載</span>
        </el-link>
      </template>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      upload: {
        // 上傳的地址
        url: process.env.VUE_APP_BASE_API + "/common/upload",
        // 上傳的文件列表
        fileList: []
      }
    }
  },
  props: {
    accessory: {
      type: String,
      default: '[]'
    }
  },
  created () {
    this.getInfo(this.accessory)
  },
  methods: {
    getInfo (accessory) {
      this.upload.fileList = []
      if (accessory !== '[]' || accessory !== null) {
        let list = JSON.parse(accessory)
        list.map(val => {
          this.upload.fileList.push({
            name: val.name,
            url:process.env.VUE_APP_BASE_API + val.url
          })
        })
      }
    },
    // 點(diǎn)擊上傳----------------文件下載
    handlePreview (file) {
      var a = document.createElement('a');
      var event = new MouseEvent('click');
      a.download = file.name;
      a.href = file.url;
      a.dispatchEvent(event);
    }
  }
};
</script>
<style lang="scss"   scoped>
::v-deep .el-upload-list {
  height: auto;
  overflow: hidden;
}
.detail {
  line-height: 20px;
  display: block;
  .icon-emad- {
    color: rgba(98, 173, 255, 1);
  }
}
</style>

頁(yè)面中引入使用:

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue中@click.stop與@click.prevent解讀

    Vue中@click.stop與@click.prevent解讀

    Vue中,`@click.stop`用于阻止事件冒泡,而`@click.prevent`用于阻止事件的默認(rèn)行為,這兩個(gè)方法在處理事件時(shí)非常有用
    2025-02-02
  • vue2.0+webpack環(huán)境的構(gòu)造過程

    vue2.0+webpack環(huán)境的構(gòu)造過程

    本文分步驟給大家介紹了vue2.0+webpack環(huán)境的構(gòu)造過程的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-11-11
  • Antd-vue Table組件添加Click事件,實(shí)現(xiàn)點(diǎn)擊某行數(shù)據(jù)教程

    Antd-vue Table組件添加Click事件,實(shí)現(xiàn)點(diǎn)擊某行數(shù)據(jù)教程

    這篇文章主要介紹了Antd-vue Table組件添加Click事件,實(shí)現(xiàn)點(diǎn)擊某行數(shù)據(jù)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • vue實(shí)現(xiàn)跨頁(yè)面定位錨點(diǎn)區(qū)域方式

    vue實(shí)現(xiàn)跨頁(yè)面定位錨點(diǎn)區(qū)域方式

    這篇文章主要介紹了vue實(shí)現(xiàn)跨頁(yè)面定位錨點(diǎn)區(qū)域方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 詳解mpvue開發(fā)微信小程序基礎(chǔ)知識(shí)

    詳解mpvue開發(fā)微信小程序基礎(chǔ)知識(shí)

    這篇文章主要介紹了詳解mpvue開發(fā)微信小程序基礎(chǔ)知識(shí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • vue三種模糊查詢方式代碼實(shí)例

    vue三種模糊查詢方式代碼實(shí)例

    這篇文章主要給大家介紹了關(guān)于vue三種模糊查詢方式的相關(guān)資料,在vue中模糊搜索主要是用computed屬性實(shí)現(xiàn),文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • 如何防止Vue組件重復(fù)渲染的方法示例

    如何防止Vue組件重復(fù)渲染的方法示例

    在 Vue.js 中,組件的重復(fù)渲染是一個(gè)常見的問題,可能會(huì)影響應(yīng)用的性能和用戶體驗(yàn),為了提升應(yīng)用的性能,開發(fā)者需要理解 Vue 的渲染機(jī)制,并應(yīng)用有效的方法來避免不必要的組件重渲染,本文將深入探討如何防止 Vue 組件重復(fù)渲染,并提供相關(guān)示例代碼,需要的朋友可以參考下
    2024-10-10
  • vue?vue-touch移動(dòng)端手勢(shì)詳解

    vue?vue-touch移動(dòng)端手勢(shì)詳解

    這篇文章主要介紹了vue?vue-touch移動(dòng)端手勢(shì)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 詳解vue-flickity的fullScreen功能實(shí)現(xiàn)

    詳解vue-flickity的fullScreen功能實(shí)現(xiàn)

    這篇文章主要介紹了詳解vue-flickity的fullScreen功能實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Vue--keep-alive使用實(shí)例詳解

    Vue--keep-alive使用實(shí)例詳解

    這篇文章主要介紹了Vue--keep-alive使用實(shí)例詳解,keep-alive標(biāo)簽主要用于保留組件狀態(tài)或避免重新渲染,用示例代碼介紹Vue的keep-alive的用法,需要的朋友可以參考下
    2022-08-08

最新評(píng)論

武穴市| 邵武市| 洮南市| 杨浦区| 兰西县| 临洮县| 镇远县| 晋城| 贺兰县| 托克逊县| 津南区| 深泽县| 揭西县| 青海省| 荆州市| 通化市| 达尔| 上蔡县| 紫阳县| 马尔康县| 邳州市| 雅江县| 抚宁县| 大埔县| 昆山市| 格尔木市| 苍梧县| 阿拉善右旗| 陆良县| 大理市| 沈丘县| 中宁县| 若尔盖县| 堆龙德庆县| 昔阳县| 高要市| 衡东县| 伊吾县| 根河市| 昂仁县| 仙桃市|