翼度科技»论坛 编程开发 JavaScript 查看内容

如何通过Vue3+Element Plus自定义弹出框组件

9

主题

9

帖子

27

积分

新手上路

Rank: 1

积分
27
前言

在Vue 3项目中,使用Element Plus构建弹出框是一项常见的任务。为了简化这个过程,我们可以封装一个公共组件,使弹出框的调用变得简单而高效。本文将介绍如何通过Vue 3和Element Plus,使用一个自定义的弹出框组件实现这一目标。

1. 弹出框组件封装

首先,我们封装了一个通用的弹出框组件,具体实现位于
  1. util.js
复制代码
文件中。通过
  1. initInstance
复制代码
方法,我们可以动态创建一个弹出框实例,并将其挂载到指定的容器上。以下是简要代码:
  1. // util.js

  2. import { h, render } from 'vue';

  3. export function initInstance(component, container, option) {
  4.     const vNode = h(component, option);
  5.     render(vNode, container);
  6.     document.body.appendChild(container.firstElementChild);
  7.     return vNode.component;
  8. }

  9. export function getContainer() {
  10.     return document.createElement('div');
  11. }
复制代码
2. 自定义弹出框配置

接下来,我们定义了一个名为
  1. portPop
复制代码
的自定义弹出框配置。这个配置使用了前面封装的通用方法,同时为弹出框提供了一些特定的配置。以下是简要代码:
  1. // common.js

  2. import { initInstance, getContainer } from "./util";
  3. import PortPop from "../components/PortPop";

  4. const instanceMap = new Map();

  5. export const portPop = (option, call) => {
  6.     const container = getContainer();
  7.     let opt = {
  8.         ...option,
  9.         onComfrim: (data) => {
  10.             call(data);
  11.         },
  12.         onVanish: () => {
  13.             render(null, container);
  14.             instanceMap.delete(vm);
  15.         },
  16.     };
  17.     const component = initInstance(PortPop, container, opt);
  18.     const vm = component.proxy;
  19.     component.exposed.openDialog();
  20.     instanceMap.set(vm, { option: opt });
  21. };
复制代码
3. 弹出框组件实现

首先,我们要先封装一下el-dialog组件
  1. <template>
  2.   <el-dialog
  3.     :model-value="modelValue"
  4.     :title="title"
  5.     :width="width"
  6.     :modal="modal"
  7.     draggable
  8.     destroy-on-close
  9.     :close-on-click-modal="false"
  10.     append-to-body
  11.     :before-close="beforeClose"
  12.     @close="dialogColse"
  13.     @closed="dialogColsed"
  14.   >
  15.     <slot />
  16.     <template #footer>
  17.       <span class="dialog-footer">
  18.         <slot name="footer" />
  19.       </span>
  20.     </template>
  21.   </el-dialog>
  22. </template>
  23. <script setup name="Dialog">
  24. import { ElDialog } from "element-plus";
  25. const props = defineProps({
  26.   modelValue: {
  27.     type: [Boolean],
  28.     default: false,
  29.   },
  30.   title: {
  31.     type: [String],
  32.     default: "",
  33.   },
  34.   width: {
  35.     type: [String],
  36.     default: "",
  37.   },
  38.   modal: {
  39.     type: [Boolean],
  40.     default: false,
  41.   },
  42.   beforeClose: [Function],
  43. });
  44. const emits = defineEmits(["update:modelValue", "onClosed", "closed"]);
  45. function dialogColse() {
  46.   emits("update:modelValue", false);
  47. }
  48. function dialogColsed() {
  49.   emits("onClosed", false);
  50.   emits("closed", false);
  51. }
  52. </script>

  53. <style lang="scss">
  54. </style>
复制代码
最后,我们实现了具体的弹出框组件
  1. PortPop
复制代码
,这个组件使用了 Element Plus 的
  1. Dialog
复制代码
组件,并在其中嵌套了一些其他组件,以实现特定的功能。以下是简要代码:
  1. <template>
  2.   <div>
  3.     <Dialog :title="props.title" v-model="dialog" width="920px">
  4.         <!-- ... 弹出框内容 ... -->
  5.         <template #footer>
  6.             <div class="dialog-footer">
  7.               <el-button type="primary" size="small" @click="confrim">
  8.                 确定
  9.               </el-button>
  10.               <el-button @click="dialog = false" size="small">取消</el-button>
  11.             </div>
  12.           </template>
  13.     </Dialog>
  14.   </div>
  15. </template>

  16. <script setup lang="tsx">
  17. import {
  18.   nextTick,
  19.   ref, useAttrs,
  20. } from 'vue'
  21. import Dialog from '@/components/dialog'
  22. import {
  23.   ElInput,
  24.   ElButton,
  25. } from 'element-plus'

  26. const props = defineProps({
  27.   title: {
  28.     type: [String],
  29.     default: '',
  30.   },
  31.   type: {
  32.     type: [String],
  33.     default: '',
  34.   },
  35. })

  36. const emits = defineEmits(['comfrim'])
  37. const attrs = useAttrs()
  38. const dialog = ref(false)
  39. function openDialog() {
  40.   dialog.value = true
  41. }
  42. const confrim = async () => {
  43.   emits('comfrim', "传递给父组件的数据")
  44.   nextTick(() => {
  45.     dialog.value = false
  46.   })
  47. }
  48. defineExpose({ openDialog })

  49. </script>
  50. <style lang="scss">
  51. </style>
复制代码
4. 使用自定义弹出框组件

最终,我们可以在需要调用弹出框的地方,简单地执行
  1. portPop
复制代码
方法,并传递相应的配置和回调函数。这样,弹出框就会显示出来,用户可以与之交互。
  1. // 在需要的地方调用自定义弹出框
  2. import { portPop } from "./customDialog";

  3. // 示例调用
  4. portPop({ title: '自定义弹出框', defaultKeyword: '关键字' }, (data) => {
  5.     // 处理弹出框确认后的数据
  6.     console.log('用户选择的数据:', data);
  7. });
复制代码
文件结构示例



组定义组件文件示例



效果:



总结

到此这篇关于如何通过Vue3+Element Plus自定义弹出框组件的文章就介绍到这了,更多相关Vue3 ElementPlus自定义弹出框组件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

来源:https://www.jb51.net/javascript/320942ru6.htm
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

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

x

举报 回复 使用道具