element ui的el-upload的picture-card类型实现单图片选择上传

这是一个dialog组件,需要传入dialog:{show:true, title: ‘标题’}和form:{}才能显示

<template>
  <div class='dialog-module'>
    <el-dialog width="300px"
               destroy-on-close
               :title="dialog.title + '模块'"
               :visible.sync="dialog.show"
               :before-close="() => { dialog.show = false }">

      <el-form :model="form"
               ref="form"
               hide-required-asterisk
               label-width="84px">
        <div class="form__head">
          <!-- 图片显示 -->
          <el-image :src="form.icon"
                    class="form__head-icon">
            <div slot="error"
                 class="image-slot">
              <i class="el-icon-picture-outline"
                 style="font-size:32px"></i>
            </div>
          </el-image>
          <!-- 图片上传 -->
          <el-upload class="form__head-icon-upload"
                     action="https://jsonplaceholder.typicode.com/posts/"
                     list-type="picture-card"
                     :class="{hide:hideUpload}"
                     :file-list="fileList"
                     :limit="1"
                     :auto-upload="false"
                     :on-change="handleChange"
                     :on-preview="handlePictureCardPreview"
                     :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
        </div>
        
        <el-form-item label="模块名称:"
                      prop="label"
                      :rules="{ required: true, message: '模块名不能为空', trigger: 'blur' }">
          <el-input v-model="form.label"
                    style="width:100%;"
                    placeholder="请输入模块名称"></el-input>
        </el-form-item>

        <el-form-item label="链接:"
                      prop="link"
                      :rules="{ required: true, message: '链接不能为空', trigger: 'blur' }">
          <el-input v-model="form.link"
                    style="width:100%;"
                    placeholder="请输入链接"></el-input>
        </el-form-item>

      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialog.show = false">取消</el-button>
        <el-button type="primary"
                   @click="handle('form')">{{ dialog.title }}</el-button>
      </span>

      <el-dialog :visible.sync="dialogVisible">
        <img width="100%"
             :src="dialogImageUrl"
             alt="">
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: 'DialogModule',
  props: {
    form: Object,
    dialog: Object
  },
  data () {
    return {
      action: '',
      hideUpload: false, // 控制选择框是否显示的开关
      fileList: [],
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  methods: {
    handleChange (file, fileList) {
      this.hideUpload = fileList.length > 0
    },
    handleRemove (file, fileList) { // 防止出场动画未结束,选择框提前出现
      setTimeout(() => {
        this.hideUpload = fileList.length > 0
      }, 520)
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    }
  }
}
</script>

<style scoped>
.dialog-module >>> .el-upload--picture-card {
  width: 100px;
  height: 100px;
  line-height: 100px;
}

.form__head {
  display: flex;
  justify-content: space-around;
  padding: 10px 2px;
  flex-wrap: nowrap;
}

.form__head-icon-upload {
  height: 100px;
  width: 100px;
}

.dialog-module >>> .form__head-icon {
  width: 100px;
  height: 100px;
  border: 1px solid #c0ccda;
  text-align: center;
  line-height: 100px;
  box-sizing: border-box;
  border-radius: 4px;
  flex-shrink: 1;
}

.dialog-module >>> .el-upload-list__item {
  width: 100px;
  height: 100px;
}
</style>

<style>
// 控制显示的主要css
.hide .el-upload--picture-card {
  display: none;
}
</style>

效果展示
在这里插入图片描述

您好,关于 element-ui上传组件 el-upload,您可以通过自定义 el-upload-list 来实现自定义上传列表的功能。 具体实现方法如下: 1. 在 el-upload 组件中添加属性 list-type="picture-card",并设置属性 action 为上传文件的地址。 2. 在 el-upload 组件中添加自定义 slot,用于自定义上传列表的显示。 3. 在自定义的上传列表组件中,使用 el-upload-list 组件来展示上传文件的列表。 4. 可以通过监听 el-upload 组件的 change 事件来获取上传的文件信息,并将上传的文件信息传递给自定义的上传列表组件,以展示上传文件的列表。 以下是一个简的示例代码: ```html <template> <div> <el-upload class="upload-demo" action="/upload" list-type="picture-card" :on-change="handleChange" > <i class="el-icon-plus"></i> <div class="upload-text">上传图片</div> </el-upload> <custom-upload-list :file-list="fileList"></custom-upload-list> </div> </template> <script> import CustomUploadList from './CustomUploadList.vue'; export default { components: { CustomUploadList, }, data() { return { fileList: [], }; }, methods: { handleChange(file, fileList) { this.fileList = fileList; }, }, }; </script> ``` CustomUploadList.vue 组件的代码如下: ```html <template> <el-upload-list class="custom-upload-list" :file-list="fileList"> <template slot-scope="{file}"> <div class="custom-list-item"> <img :src="file.url" class="custom-list-item-thumbnail"> <div class="custom-list-item-name">{{ file.name }}</div> <div class="custom-list-item-actions"> <el-button size="small" type="text" @click="handleRemove(file)">删除</el-button> </div> </div> </template> </el-upload-list> </template> <script> export default { props: { fileList: { type: Array, default: () => [], }, }, methods: { handleRemove(file) { const index = this.fileList.indexOf(file); if (index !== -1) { this.fileList.splice(index, 1); } }, }, }; </script> ``` 希望这个示例能够帮助到您。如果您有任何问题,请随时提出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值