IT俱乐部 JavaScript 如何通过Vue3+Element Plus自定义弹出框组件

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

前言

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

1. 弹出框组件封装

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 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. 自定义弹出框配置

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 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. 弹出框组件实现

首先,我们要先封装一下el-dialog组件 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<span class="dialog-footer">
        </span>
     
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);
}

最后,我们实现了具体的弹出框组件 PortPop,这个组件使用了 Element Plus 的 Dialog 组件,并在其中嵌套了一些其他组件,以实现特定的功能。以下是简要代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<div>
    <div class="dialog-footer">
               
                确定
              取消
</div>
           
</div>
 
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', "传递给父组件的数据")
  nextTick(() => {
    dialog.value = false
  })
}
defineExpose({ openDialog })

4. 使用自定义弹出框组件

最终,我们可以在需要调用弹出框的地方,简单地执行portPop方法,并传递相应的配置和回调函数。这样,弹出框就会显示出来,用户可以与之交互。

1
2
3
4
5
6
7
8
// 在需要的地方调用自定义弹出框
import { portPop } from "./customDialog";
 
// 示例调用
portPop({ title: '自定义弹出框', defaultKeyword: '关键字' }, (data) => {
    // 处理弹出框确认后的数据
    console.log('用户选择的数据:', data);
});

文件结构示例

组定义组件文件示例

效果:

总结 

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

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/navsub/js/11092.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部