欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue?+?Element?自定義上傳封面組件功能

 更新時間:2023年01月10日 16:28:20   作者:尚尚是高尚的尚  
這篇文章主要介紹了Vue?+?Element?自定義上傳封面組件,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前一段時間做項目,頻繁使用到上傳圖片組件,而且只上傳一個封面,于是想著自定義一個圖片封面上傳組件。先來看一下效果:

第一張圖片是上傳之前,第二張圖片是上傳成功后,第3張圖片是鼠標放上去之后的效果!首先整理需求,圖片上傳我們使用照片墻的方式,只能上傳一張圖片,圖片上傳成功后不能繼續(xù)上傳,如果想要更換圖片,則需要將圖片刪除后重新上傳。點擊圖片上面的放大鏡可以查看大圖。需要限制圖片上傳的格式,圖片的大小。組件代碼:

<template>
  <div class="upload">
    <el-upload
      :class="{'hidden':mFileList.length > 0}"
      list-type="picture-card"
      :on-remove="handleRemove"
      :action="action"
      :before-upload="beforeUploadHandle"
      :on-success="successHandle"
      :on-change="changeHandle"
      :limit="1"
      :accept="accept"
      :on-exceed="handleExceed"
      :file-list="fileList"
      :on-preview="handlePictureCardPreview"
    >
      <i class="el-icon-plus"></i>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    action: {
      type: String,
      default: "",
    },
    accept: {
      type: String,
      default: "",
    },
    fileList:{
      type: Array,
      default: () => [],
    },
  },
  watch: {
    fileList(newValue, oldValue) {
      this.mFileList = newValue
    }
  },
  data() {
    return {
      dialogVisible: false, //圖片放大
      fileImg: "", //上傳圖片
      dialogImageUrl: "", //圖片地址
      mFileList:this.fileList,
    };
  },
  methods: {
    handleRemove(file, fileList) {
      this.$emit("upload-remove", file);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 上傳之前
    beforeUploadHandle(file) {
      if (file.type !== "image/jpeg" && file.type !== "image/png") {
        this.$message({
          message: "只支持jpg、png格式的圖片!",
          type: "warning",
        });
        return false;
      }
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message({
          message: "上傳文件大小不能超過 2MB!",
          type: "warning",
        });
        return false;
      }
    },
    // 上傳成功
    successHandle(response, file, fileList) {
      this.mFileList = fileList;
      if (response && response.code === 200) {
        this.$message.success("圖片上傳成功!");
        this.$emit("upload-success", response, file);
      } else {
        this.$message.error(response.msg);
      }
    },
    changeHandle(file, fileList) {
      if(file.response && file.response.code == 500) {
         this.$emit("upload-error",file);
      }
    },
    handleExceed(files, fileList) {
        this.$message.warning("只能上傳1張圖片!");
      },
  },
};
</script>
<style lang="scss">
.upload .hidden .el-upload--picture-card {
  display: none;
}
</style>

調(diào)用組件代碼:

<template>
    <div>
        <el-form ref="dataForm"    label-width="80px">
            <el-form-item label="封面" prop="cover" class="is-required">
                <upload list-type="picture-card" :action="url" :accept="'.jpg,.png,.JPG,.PNG'" :fileList="fileList"
                    :limit="1" @upload-success="uploadFile" @upload-remove="removeFile" @upload-error="uploadError">
                </upload>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
    import Upload from '../components/cover-upload/index.vue'
    export default {
        components: {
            Upload
        },
        data() {
            return {
                url: "",
                fileList: [],
            }
        },
        methods: {
            uploadUrl() {
                this.url = "http://xxx.xxx.xxx.xxx:xxx/yyxt/admin/course/courseInfo/upload?token=075de0303b15a38833a30a7a3b494794"http://上傳圖片的后臺接口
            },
            uploadError(file) {
                this.fileList = [];
            },
            uploadFile(response, file) {
                this.fileList = [{
                    url: response.data,
                }, ];
            },
            removeFile(file) {
                this.fileList = [];
            },
        },
        mounted() {
            this.uploadUrl();
        }
    }
</script>

點擊上傳后的圖片上的放大鏡,顯示圖片大圖

到此這篇關(guān)于Vue + Element 自定義上傳封面組件的文章就介紹到這了,更多相關(guān)Vue + Element 自定義上傳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論