国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

vue Element ui上傳組件el-upload封裝

這篇具有很好參考價值的文章主要介紹了vue Element ui上傳組件el-upload封裝。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

el-upload組件二次封裝

注釋:
1.limit可上傳圖片數(shù)量
2.lableName當(dāng)前組件name,用于一個頁面多次使用上傳組件,對數(shù)據(jù)進(jìn)行區(qū)分
3.upload上傳圖片發(fā)生變化觸發(fā),返回已上傳圖片的信息
4.imgUrl默認(rèn)圖片文章來源地址http://www.zghlxwxcb.cn/news/detail-512278.html

<template>
  <div class="uploadimg " :style="{marginLeft: marginLeft, }">
    <el-upload
    action="false"
    :class="isAddImg ? 'disabled' : ''"
    accept="image/png,image/gif,image/jpg,image/jpeg"
    :limit="limit"
    :with-credentials="true"
    :multiple="false"
    list-type="picture-card"
    :before-upload="beforeUpload"
    :file-list="fileList"
    :http-request="httpRequest"
    :on-preview="handlePictureCardPreview"
    :on-remove="onRemove"
    >
      <div class="uploadIco">
        <i class="el-icon-plus" ></i>
        <div class="text">{{uploadType}}</div>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible1" width="600px" append-to-body class="uploadimgDialog" >
      <img width="100%" :src="dialogImageUrl" />
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    limit: {
      type: Number,
      default: 1,
    },
    imgUrl: {
      type: String,
      required:true,
    },
    lableName: {
      type: String,
      default: '',
    },
    uploadType:{
      type: String,
       default: '上傳圖片',
    },
    marginLeft:{
      type: String,
       default: '0px',
    }
  },
  data() {
    return {
      fileArr:[],
      fileList: [],
      isRemove: false, // 用于編輯時是否上傳了圖標(biāo)
      dialogVisible1: false,
      dialogImageUrl: "",
    };
  },
  computed:{
    isAddImg(){
      if(this.limit == this.fileArr.length){
          return true
      }else{
          return false
      }
    },
  },
  watch:{
    imgUrl:{
      immediate: true,
      deep: true,
      handler(val,oldVal){
        if(val!=''){
          this.dialogImageUrl = val;
          this.fileList = [{ url: val }];
          this.fileArr = [{ url: val }];
        }else{
          this.dialogImageUrl = '';
          this.fileList = [];
        }
      },
    },
  },
  methods: {

    //限制圖片格式及大小
    beforeUpload(file) {
      const isJPG =
        file.type === "image/jpeg" ||
        file.type === "image/png" ||
        file.type === "image/jpg" ||
        file.type === "image/gif";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$Message.error("上傳圖片只能是 png、gif、jpeg、jpg 格式!");
      } else if (!isLt2M) {
        this.$Message.error("上傳圖片大小不能超過 2MB!");
      }
      return isJPG && isLt2M;
    },

    //上傳后獲取文件流
    async httpRequest(file) {
      this.fileArr.push(file.file)
      this.$emit("upload", { fileArr:this.fileArr , type:'add',lableName:this.lableName });
    },

    //預(yù)覽
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible1 = true;
    },

    //刪除
    onRemove(file, fileList) {
      let arr = []
      fileList.forEach((item)=>{
        if(item.raw){
          arr.push(item.raw)
        }else{
          arr.push(item)
        }
      });
      this.fileArr = arr;
      this.$emit("upload", { fileArr:this.fileArr , type:'del',lableName:this.lableName });
    },
  },
};
</script>
<style lang="scss" scoped>
.uploadimg{
  .uploadIco {
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    line-height: 17px !important;
  }
}
::v-deep .disabled .el-upload{
  display: none;
}
</style>

組件使用

<template>
  <div>
    <UploadImg
    :limit="2"
    :lableName="'authorizationLetter'"
    @upload='receiveFile'
    :imgUrl='ruleForm.imgUrl' />
  </div>
</template>

<script>
export default {
  data(){
    return {
      ruleForm:{
        imgUrl:'',
      }
    }
  },
  components: {
      UploadImg: () => import("./component/UploadImg.vue"),
  },
  methods:{
    receiveFile(data){
      //如果設(shè)置了默認(rèn)圖片請注意data返回值
      //data返回值是一個數(shù)組,數(shù)組中如果設(shè)置了默認(rèn)值data返回的數(shù)據(jù)會存在對象和文件流兩種數(shù)據(jù)類型
      //如果數(shù)據(jù)中存在name就是一個文件流,如果沒有就是設(shè)置的默認(rèn)值的數(shù)據(jù)
      console.log("data",data)
    },
  }
}
</script>


到了這里,關(guān)于vue Element ui上傳組件el-upload封裝的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包