选择图片的弹窗组件

代码

<template>
  <el-dialog :visible.sync="dialogVisible" title="选择图片" width="50%">
    <el-upload
      class="upload-demo"
      :action="actionUrl"
      :headers="headers"
      :data="{ token: token }"
      :on-success="handleSuccess"
      :on-error="handleError"
      :on-remove="handleRemove"
      :file-list="fileList"
      :auto-upload="false"
      :before-upload="beforeUpload"
      :limit="limit"
      :accept="accept"
      :multiple="multiple"
    >
      <el-button size="small" type="primary">点击上传</el-button>
      <div slot="tip" class="el-upload__tip">只能上传{{ limit }}个文件,且不超过 {{ size }} MB</div>
    </el-upload>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleConfirm">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'ImageSelector',
  props: {
    actionUrl: String, // 上传图片的接口地址
    headers: Object, // 上传图片时需要携带的请求头部信息
    token: String, // 上传图片时需要携带的 token
    limit: { // 最多上传的图片数量
      type: Number,
      default: 1
    },
    size: { // 单张图片最大的大小,单位为 MB
      type: Number,
      default: 10
    },
    accept: { // 接受上传的图片类型
      type: String,
      default: 'image/*'
    },
    multiple: { // 是否支持多选
      type: Boolean,
      default: false
    },
    defaultList: { // 默认的图片列表
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      dialogVisible: false,
      fileList: []
    }
  },
  methods: {
    // 点击确定按钮
    handleConfirm() {
      this.$emit('confirm', this.fileList)
      this.dialogVisible = false
    },
    // 上传成功回调
    handleSuccess(response, file, fileList) {
      this.fileList = fileList
    },
    // 上传失败回调
    handleError(err, file, fileList) {
      this.$message.error('上传失败')
    },
    // 移除文件回调
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    // 上传前校验回调
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < this.size

      if (!isJPG) {
        this.$message.error('上传图片只能是 JPG/PNG 格式!')
        return false
      }
      if (!isLt2M) {
        this.$message.error(`上传图片大小不能超过 ${this.size} MB!`)
        return false
      }
      return true
    },
    // 打开弹窗
    openDialog() {
      this.dialogVisible = true
    },
    // 关闭弹窗
    closeDialog() {
      this.dialogVisible = false
    }
  },
  mounted() {
    this.fileList = this.defaultList
  }
}
</script>

<style>
.el-upload__tip {
  font-size: 12px;
}
</style>

如何使用

<template>
  <div>
    <el-button @click="openImageSelector">选择图片</el-button>
    <image-selector :actionUrl="uploadUrl" :defaultList="imageList" @confirm="handleImageSelect"></image-selector>
  </div>
</template>

<script>
import ImageSelector from './ImageSelector'

export default {
  components: {
    ImageSelector
  },
  data() {
    return {
      uploadUrl: 'http://localhost:8080/upload',
      imageList: []
    }
  },
  methods: {
    openImageSelector() {
      this.$refs.imageSelector.openDialog()
    },
    handleImageSelect(fileList) {
      this.imageList = fileList
    }
  }
}
</script>

使用时需要传入上传图片的接口地址,以及其他相关配置信息。通过 openDialog() 方法打开弹窗,选择完图片后,点击弹窗中的“确定”按钮,会触发 confirm 事件,将选择的图片列表作为参数传递出去。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基本的 Vue 弹窗组件: ``` <template> <div class="modal" v-if="show"> <div class="modal-overlay" @click="close"></div> <div class="modal-container"> <div class="modal-header"> <slot name="header"></slot> <button class="modal-close" @click="close">×</button> </div> <div class="modal-body"> <slot></slot> </div> <div class="modal-footer"> <slot name="footer"></slot> </div> </div> </div> </template> <script> export default { props: { show: { type: Boolean, default: false } }, methods: { close() { this.$emit('close'); } } } </script> <style> .modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .modal-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } .modal-container { background-color: #fff; width: 500px; max-width: 100%; border-radius: 4px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3); overflow: hidden; } .modal-header { display: flex; justify-content: space-between; align-items: center; padding: 16px; border-bottom: 1px solid #ccc; } .modal-close { font-size: 24px; font-weight: bold; color: #ccc; background-color: transparent; border: none; cursor: pointer; outline: none; } .modal-body { padding: 16px; } .modal-footer { display: flex; justify-content: flex-end; align-items: center; padding: 16px; border-top: 1px solid #ccc; } </style> ``` 这个组件包含了一个遮罩层、一个弹窗容器和三个插槽(header、body、footer),可以用于显示不同的内容,比如提示信息、表单、图片等。你可以根据自己的需要进行修改和扩展。在父组件中可以通过 v-modal 指令来控制弹窗的显示和隐藏: ``` <template> <div> <button @click="showModal = true">打开弹窗</button> <modal :show="showModal" @close="showModal = false"> <template v-slot:header> <h2>标题</h2> </template> <template v-slot:body> <p>内容</p> </template> <template v-slot:footer> <button @click="showModal = false">关闭弹窗</button> </template> </modal> </div> </template> <script> import Modal from './Modal.vue'; export default { components: { Modal }, data() { return { showModal: false } } } </script> ``` 这个例子中,当点击按钮时,会将 showModal 属性设置为 true,弹出弹窗。当点击弹窗的关闭按钮或遮罩层时,会将 showModal 属性设置为 false,关闭弹窗

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值