如何通過Vue3+Element?Plus自定義彈出框組件
前言
在Vue 3項(xiàng)目中,使用Element Plus構(gòu)建彈出框是一項(xiàng)常見的任務(wù)。為了簡化這個(gè)過程,我們可以封裝一個(gè)公共組件,使彈出框的調(diào)用變得簡單而高效。本文將介紹如何通過Vue 3和Element Plus,使用一個(gè)自定義的彈出框組件實(shí)現(xiàn)這一目標(biāo)。
1. 彈出框組件封裝
首先,我們封裝了一個(gè)通用的彈出框組件,具體實(shí)現(xiàn)位于 util.js
文件中。通過 initInstance
方法,我們可以動(dòng)態(tài)創(chuàng)建一個(gè)彈出框?qū)嵗?,并將其掛載到指定的容器上。以下是簡要代碼:
// util.js import { h, render } from 'vue'; export function initInstance(component, container, option) { const vNode = h(component, option); render(vNode, container); document.body.appendChild(container.firstElementChild); return vNode.component; } export function getContainer() { return document.createElement('div'); }
2. 自定義彈出框配置
接下來,我們定義了一個(gè)名為 portPop
的自定義彈出框配置。這個(gè)配置使用了前面封裝的通用方法,同時(shí)為彈出框提供了一些特定的配置。以下是簡要代碼:
// common.js import { initInstance, getContainer } from "./util"; import PortPop from "../components/PortPop"; const instanceMap = new Map(); export const portPop = (option, call) => { const container = getContainer(); let opt = { ...option, onComfrim: (data) => { call(data); }, onVanish: () => { render(null, container); instanceMap.delete(vm); }, }; const component = initInstance(PortPop, container, opt); const vm = component.proxy; component.exposed.openDialog(); instanceMap.set(vm, { option: opt }); };
3. 彈出框組件實(shí)現(xiàn)
首先,我們要先封裝一下el-dialog組件
<template> <el-dialog :model-value="modelValue" :title="title" :width="width" :modal="modal" draggable destroy-on-close :close-on-click-modal="false" append-to-body :before-close="beforeClose" @close="dialogColse" @closed="dialogColsed" > <slot /> <template #footer> <span class="dialog-footer"> <slot name="footer" /> </span> </template> </el-dialog> </template> <script setup name="Dialog"> import { ElDialog } from "element-plus"; const props = defineProps({ modelValue: { type: [Boolean], default: false, }, title: { type: [String], default: "", }, width: { type: [String], default: "", }, modal: { type: [Boolean], default: false, }, beforeClose: [Function], }); const emits = defineEmits(["update:modelValue", "onClosed", "closed"]); function dialogColse() { emits("update:modelValue", false); } function dialogColsed() { emits("onClosed", false); emits("closed", false); } </script> <style lang="scss"> </style>
最后,我們實(shí)現(xiàn)了具體的彈出框組件 PortPop
,這個(gè)組件使用了 Element Plus 的 Dialog
組件,并在其中嵌套了一些其他組件,以實(shí)現(xiàn)特定的功能。以下是簡要代碼:
<template> <div> <Dialog :title="props.title" v-model="dialog" width="920px"> <!-- ... 彈出框內(nèi)容 ... --> <template #footer> <div class="dialog-footer"> <el-button type="primary" size="small" @click="confrim"> 確定 </el-button> <el-button @click="dialog = false" size="small">取消</el-button> </div> </template> </Dialog> </div> </template> <script setup lang="tsx"> import { nextTick, ref, useAttrs, } from 'vue' import Dialog from '@/components/dialog' import { ElInput, ElButton, } from 'element-plus' const props = defineProps({ title: { type: [String], default: '', }, type: { type: [String], default: '', }, }) const emits = defineEmits(['comfrim']) const attrs = useAttrs() const dialog = ref(false) function openDialog() { dialog.value = true } const confrim = async () => { emits('comfrim', "傳遞給父組件的數(shù)據(jù)") nextTick(() => { dialog.value = false }) } defineExpose({ openDialog }) </script> <style lang="scss"> </style>
4. 使用自定義彈出框組件
最終,我們可以在需要調(diào)用彈出框的地方,簡單地執(zhí)行portPop
方法,并傳遞相應(yīng)的配置和回調(diào)函數(shù)。這樣,彈出框就會(huì)顯示出來,用戶可以與之交互。
// 在需要的地方調(diào)用自定義彈出框 import { portPop } from "./customDialog"; // 示例調(diào)用 portPop({ title: '自定義彈出框', defaultKeyword: '關(guān)鍵字' }, (data) => { // 處理彈出框確認(rèn)后的數(shù)據(jù) console.log('用戶選擇的數(shù)據(jù):', data); });
文件結(jié)構(gòu)示例
組定義組件文件示例
效果:
總結(jié)
到此這篇關(guān)于如何通過Vue3+Element Plus自定義彈出框組件的文章就介紹到這了,更多相關(guān)Vue3 ElementPlus自定義彈出框組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js數(shù)據(jù)加載完成前顯示原代碼{{代碼}}問題及解決
這篇文章主要介紹了vue.js數(shù)據(jù)加載完成前顯示原代碼{{代碼}}問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07webpack搭建vue環(huán)境時(shí)報(bào)錯(cuò)異常解決
這篇文章主要介紹了webpack搭建vue環(huán)境時(shí)報(bào)錯(cuò)異常解決,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09vue中實(shí)現(xiàn)點(diǎn)擊空白區(qū)域關(guān)閉彈窗的兩種方法
這篇文章主要介紹了vue中實(shí)現(xiàn)點(diǎn)擊空白區(qū)域關(guān)閉彈窗的兩種方法,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12Vue初始化中的選項(xiàng)合并之initInternalComponent詳解
這篇文章主要介紹了Vue初始化中的選項(xiàng)合并之initInternalComponent的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06vue element-ui中table合計(jì)指定列求和實(shí)例
這篇文章主要介紹了vue element-ui中table合計(jì)指定列求和實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11