简介

Vue配套的一个UI库

安装使用

插件安装:npm i element-ui --save

src/main.js入口文件中引入

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)

案例

图片上传,带请求头

<el-upload
  class="avatar-uploader"
  :action="urlUpload"
  :headers="headers"
  :show-file-list="false"
  :on-success="handleAvatarSuccess">
  <img v-if="url" :src="url" class="avatar">
  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
import { getToken } from '@/utils/auth'

export default {
  props: {
    name: {
      type: String,
      default: ''
    },
    url: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      headers: {
        "QQ-Token" : getToken()
      }
    };
  },
  computed: {
    urlUpload() {
      return process.env.BASE_API + '/xxx'
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      if (res.code === 0) {
        this.$emit('success', res.data.url, this.name)
      } else {
        this.$message.error(res.msg)
        // this.$refs.imageUpload.uploadFiles = this.$refs.imageUpload.uploadFiles.filter((item, index) => {
        //   if (item.response !== undefined) {
        //     return item.response.code === 0
        //   } else {
        //     return true
        //   }
        // })
      }  
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    }
  }
}

mp3文件上传(base64格式)

<el-upload
  ref="upload"
  class="upload-demo"
  drag
  :multiple="false"
  :file-list="fileList"
  :limit="1"
  accept=".mp3"
  :http-request="handleMp3Upload"
>
<!-- :auto-upload="false" :on-success="handleSuccess" -->
  <i class="el-icon-upload"></i>
  <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
</el-upload>
import fetch from '@/utils/fetch'

export default {
  data() {
    return {
      fileList: [],
      headers: {
        "QQ-Token" : getToken()
      }
    }
  },
  methods: {
    handleMp3Upload() {
      let file = this.$refs.upload.uploadFiles[0].raw
      let name = file.name
      // 通过DOM取文件数据
      let reader = new FileReader()
      let f = file
      reader.readAsDataURL(f)
      let that = this
      reader.onload = function(e) {
        let binary = e.target.result
        //上传文件
        fetch({
          method: 'post',
          url: '/xxx',
          data: {
            name: name,
            file: binary
          }
        }).then(res => {
          if(res.code === 0) {
            that.$message.success(res.msg)
            that.$emit('success', res.data.url)
          } else {
            that.$message.error(res.msg)
            that.$refs.upload.uploadFiles = that.$refs.upload.uploadFiles.filter((item, index) => {
              if (item.response !== undefined) {
                return item.response.code === 0
              } else {
                return true
              }
            })
          }
        }).catch(error => {
          //失败继续处理
        })
      }
    }
  }
}

By lxcss

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注