Vue自定义弹窗

本文介绍了如何使用Vue的createApp函数,在#app元素后动态插入自定义的PromptModal弹窗组件,并提供关闭事件处理和弹窗打开的方法,如dialogOpen和openRemarkDialog,用于显示备注信息。
摘要由CSDN通过智能技术生成
import { createApp } from 'vue';
import PromptModal from './../../views/modals/prompt-modal/prompt-modal.vue'; // 自定义组件
import { h } from 'vue';

// 在#app最后一个元素后添加一个弹窗元素(自定义组件)
// options:弹窗组件的参数 component:弹窗组件 boxClassName弹窗最外层样式名称
export function insertElement(options = {}, component: any, boxClassName: string) {
  return new Promise((resolve) => {
    const container = document.createElement('div')
    container.classList.add(boxClassName);
    const appRoot = document.querySelector('#app')
    if (appRoot != null) {
      const lastChild = appRoot.lastElementChild;
      const modalApp = createApp({
        render: () => h(component, {
          ...options,
          // 关闭弹窗
            ['onCloseEmit']: (result: any) => {
              resolve(result);
              modalApp.unmount();
              container.remove();
            },
        }),
      });

      // 将组件挂载到 container 元素上
      modalApp.mount(container);
      if (lastChild) {
        appRoot.insertBefore(container, lastChild.nextSibling);
      } else {
        appRoot.appendChild(container);
      }
    }
  })
}

// 弹窗
export function dialogOpen({ options = {} }) {
  return insertElement(options, PromptModal, 'pop-up-box');
}

// 备注弹窗
export function openRemarkDialog(remark: string) {
   return dialogOpen({ options: { second: 10,title: '备注信息', remark: remark }})
}


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值