vue3 element-plus 实现图片预览

    • 效果

2、实现

element-plus下有这么一个组件 <el-image-viewer/>,但是这个组件是没写在文档上面的,像普通组件一样使用即可

可以通过点击按钮实现图片预览,而非el-image组件只能通过点击图片实现预览

2.1封装组件

<template>
  <div class="img-viewer-box">
    <el-image-viewer
      v-if="state.visible"
      :url-list="props.imgs"
      @close="close"
    />
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue'
import { useVModel } from '@vueuse/core'
const props = defineProps<{
  modelValue: boolean
  imgs: string[]
}>()
const emits = defineEmits<{
  (e: 'update:modelValue', data: boolean)
}>()
const state = reactive({
  imgList: [],
  // 相当于是set 与 get
  visible: useVModel(props, 'modelValue', emits),
})

// 点击关闭的时候,连同小图一起关闭
function close() {
  state.visible = false
}
</script>

<style scoped></style>

2.3组件使用

在需要使用的地方引入,然后使用即可,这不是重点,每个人使用的方式都不一样,根据自己需求来即可

重点是上面的组件封装,看明白就会用了

 <!-- 增加用于显示预览图片 -->
 <ImgPreview v-model="state.visible.modal" :imgs="[state.imageUrl]" />

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZL随心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值