element-ui拖拽上传及问题解决(drag的使用注意事项)

element-ui拖拽上传及问题解决(drag的使用注意事项)

在这里插入图片描述

上传组件(:drag=“true”)

<template>
  <el-upload
    class="avatar-uploader"
    action=""
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
    :http-request="uploadImage"
    :drag="true"
  >
    <img v-if="value" :src="value" class="avatar">
    <i v-else class="el-icon-plus avatar-uploader-icon" />
  </el-upload>

</template>

<script>
import COS from 'cos-js-sdk-v5'
export default {
  props: {
    value: {
      type: String,
      required: true
    }
  },	
  methods: {
    uploadImage(e) {
      console.log(e) // 上传的图片是e.file
      console.log(e.file)
      const cos = new COS({
        SecretId: 'AKID4zhBRirnVyCD0Zu1xfNs9XjiX9ZkhPrw',
        SecretKey: 'xhDUbVCftyrY3NXbVEpGVWK0UkmaFiCi'
      }) // 完成cos对象的初始化
      cos.putObject(
        {
          Bucket: 'web90-1318397310', // 存储桶名称
          Region: 'ap-nanjing', // 地域名称
          Key: e.file.name, // 文件名称
          StorageClass: 'STANDARD', // 固定值
          Body: e.file // 文件对象
        },
        (err, data) => {
          if (data.statusCode === 200 && data.Location) {
            console.log(data)
            // 拿到了腾讯云返回的地址
            // 通过input自定义事件将地址传出去
            this.$emit('input', 'http://' + data.Location) // 将地址返回了
          } else {
            this.$message.error(err.Message) // 上传失败提示消息
          }
        }
      )
    },
    handleAvatarSuccess(res, file) {
      //   this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG) {
        this.$message.error('上传头像图片只能是 PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.el-upload-dragger{
    width: 180px;
    height: 180px;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader-darger {
  width: 180px;
  height: 180px;
}
</style>

官方使用

<el-upload
  class="upload-demo"
  drag
  action="https://jsonplaceholder.typicode.com/posts/"
  multiple>
  <i class="el-icon-upload"></i>
  <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  <div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>

注意:

在使用drag时,会在外边形成一个class='avatar-uploader-darger’的盒子,并且该盒子宽度会变成原来的2倍.

解决方法:

在css样式中,找到类名为avatar-uploader-darger的盒子重新设置宽度,

例如:

.avatar-uploader-darger {
width: 180px;
height: 180px;
子宽度会变成原来的2倍.

解决方法:

在css样式中,找到类名为avatar-uploader-darger的盒子重新设置宽度,

例如:

.avatar-uploader-darger {
width: 180px;
height: 180px;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用 Element UI 的 `<el-table>` 组件结合拖拽库,实现一个可以上下拖拽的表格组件。以下是一个示例代码: ```vue <template> <div> <el-table :data="tableData" :row-key="getRowKey" ref="dragTable" :row-class-name="getRowClassName" > <el-table-column label="序号" width="100"> <template slot-scope="{ row, $index }"> <span>{{ $index + 1 }}</span> </template> </el-table-column> <el-table-column label="姓名"> <template slot-scope="{ row }"> <span>{{ row.name }}</span> </template> </el-table-column> <el-table-column label="年龄"> <template slot-scope="{ row }"> <span>{{ row.age }}</span> </template> </el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 20 }, { name: '李四', age: 25 }, { name: '王五', age: 30 }, { name: '赵六', age: 35 }, ], }; }, methods: { getRowKey(row) { return row.name; // 根据实际情况返回唯一的键值 }, getRowClassName(row, index) { return `row-${index}`; }, handleDragEnd(event) { const { newIndex, oldIndex } = event; const draggedRow = this.tableData.splice(oldIndex, 1)[0]; this.tableData.splice(newIndex, 0, draggedRow); // 后续可以在此处将新的顺序保存到数据库或进行其他操作 }, }, mounted() { const dragTable = this.$refs.dragTable.$el.querySelector('.el-table__body-wrapper'); const sortable = new Sortable(dragTable, { animation: 150, handle: '.el-table__row', ghostClass: 'dragging', }); sortable.on('end', this.handleDragEnd); }, }; </script> <style scoped> .dragging { opacity: 0.5; } </style> ``` 在上面的示例中,我们使用了 `<el-table>` 组件来展示表格数据,通过设置 `:row-key` 属性来指定行的唯一键值。然后,我们使用了一个自定义的样式类名 `row-${index}` 来为每一行添加样式,以便在拖拽时添加视觉效果。我们还使用了 Sortable.js 库来实现拖拽功能,并监听了拖拽结束的事件来更新表格数据的顺序。 注意:在使用此示例代码之前,请确保已安装 Sortable.js 库,并在 Vue 组件中进行引入。 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值