Vue3封装函数组件(ElImageViewer)预览图片

目录结构

index.vue

html 复制代码
<template>
    <el-image-viewer v-if="show" v-bind="$attrs" hide-on-click-modal @close="show = false" />
</template>
 
<script setup>
import { ref, watch } from "vue"
import { ElImageViewer } from "element-plus" //自定义函数组件无法使用全局组件,需要单独引入

const props = defineProps({
    visible: {
        type: Boolean,
        default: false,
    },
    remove: {
        type: Function, //传入createApp中移除节点的方法
        default: null,
    },
    // api文档:https://element-plus.org/zh-CN/component/image.html#image-viewer-attributes
})

const show = ref(props.visible)
// 监听显示的消失,需要移除dom
watch(() => show.value, (val) => {
    !val && props.remove()
})
</script>

index.js

js 复制代码
import { createApp } from 'vue'
import index from './index.vue'

export default (options) => {
    // 创建一个节点,并将组件挂载上去
    const root = document.createElement('div')
    document.body.appendChild(root)

    const app = createApp(index, {
        ...options, visible: true, remove() {
            app.unmount(root) //创建完后要进行销毁
            document.body.removeChild(root)
        }
    })
    return app.mount(root)
}

使用方法在js||vue文件中

js 复制代码
import previewImage from "@/fcComponents/previewImage"
previewImage({ urlList: ["https://fuss10.elemecdn.com/a/3f/3302e58f9a181d2509f3dc0fa68b0jpeg.jpeg"] })
相关推荐
前端啊2 小时前
告别 el-table 打印难题,vue3-print-el-table 来了!
前端·vue.js
AprChell4 小时前
低代码设计器和低代码设计引擎架构综述
前端·vue.js·低代码
Ruihong4 小时前
🎉 VuReact 1.9.0 发布,支持 Vue 3.4 defineModel 编译到 React
vue.js·react.js·面试
英勇无比的消炎药4 小时前
TinyRobot 源码深度分析:OpenTiny 的 AI 对话组件库
前端·vue.js·github
行者全栈架构师1 天前
UniApp集成vk-uview-ui组件库详解:打造高效UI开发体验
前端·vue.js
Csvn1 天前
Vue 3 defineModel 翻车实录:多个 v-model 绑定到底怎么写?
前端·vue.js
Momo__1 天前
VueUse createReusableTemplate —— 单文件组件内的模板复用神器
前端·vue.js
程序员小富1 天前
我开源了一个开发者专属的智能 JSON 工具,得到了媳妇高度认可
前端·vue.js·后端
JustHappy1 天前
「软件设计思想杂谈🤔」“切图仔”也能懂编译原理?框架源码也许没那么难。聊聊 Vue 的编译(上)
前端·javascript·vue.js
假如让我当三天老蒯2 天前
Options API(选项式 API) 和 Composition API(组合式 API)
前端·vue.js·面试