[web教学] vue3 element-plus 实现图片预览

[复制链接]
查看1395 | 回复0 | 2023-8-23 11:43:32 | 显示全部楼层 |阅读模式 来自 中国北京



    • 结果


      
                         2、实现

   element-plus下有这么一个组件 <el-image-viewer/>,但是这个组件是没写在文档上面的,像普通组件一样使用即可
  可以通过点击按钮实现图片预览,而非el-image组件只能通过点击图片实现预览
  2.1封装组件

  1. <template>
  2.   <div class="img-viewer-box">
  3.     <el-image-viewer
  4.       v-if="state.visible"
  5.       :url-list="props.imgs"
  6.       @close="close"
  7.     />
  8.   </div>
  9. </template>
  10. <script lang="ts" setup>
  11. import { ref, reactive } from 'vue'
  12. import { useVModel } from '@vueuse/core'
  13. const props = defineProps<{
  14.   modelValue: boolean
  15.   imgs: string[]
  16. }>()
  17. const emits = defineEmits<{
  18.   (e: 'update:modelValue', data: boolean)
  19. }>()
  20. const state = reactive({
  21.   imgList: [],
  22.   // 相当于是set 与 get
  23.   visible: useVModel(props, 'modelValue', emits),
  24. })
  25. // 点击关闭的时候,连同小图一起关闭
  26. function close() {
  27.   state.visible = false
  28. }
  29. </script>
  30. <style scoped></style>
复制代码
2.3组件使用

  在必要使用的地方引入,然后使用即可,这不是重点,每个人使用的方式都不一样,根据自己需求来即可
  重点是上面的组件封装,看明确就会用了
  1. <!-- 增加用于显示预览图片 -->
  2. <ImgPreview v-model="state.visible.modal" :imgs="[state.imageUrl]" />
复制代码


来源:https://blog.csdn.net/weixin_44832362/article/details/128821438
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则