封装一个函数式调用的el-dialog弹窗组件

// dialogPlugin.js  
import Vue from 'vue';  
import { Dialog } from 'element-ui';  
  
let DialogConstructor = Vue.extend(Dialog);  
  
let dialogInstance = null;  
  
function showDialog(options) {  
  // 如果已经存在一个Dialog实例,则先关闭它  
  if (dialogInstance && dialogInstance.close) {  
    dialogInstance.close();  
  }  
  dialogInstance = new DialogConstructor({  
    propsData: {  
      title: options.title || '提示',  
      visible: true,  
      width: options.width || '50%',  
      // 其他你需要的props属性...  
    },  
  });  
  // 挂载到body上  
  let div = document.createElement('div');  
  document.body.appendChild(div);  
  dialogInstance.$mount(div);  
  // 添加关闭事件监听,以便在关闭后销毁实例  
  dialogInstance.$once('update:visible', (newVal) => {  
    if (!newVal) {  
      dialogInstance.$destroy();  
      document.body.removeChild(div);  
      dialogInstance = null;  
    }  
  });  
  // 返回Dialog实例以便外部可以调用它的方法(比如:确认、关闭等)  
  return dialogInstance;  
}  
  
// 导出showDialog方法以便在其他地方使用  
export default showDialog;
<template>  
     <button @click="a">点击我触发A组件的方法</button>
  </template>  
    
  <script>  
 
 import Vue from 'vue';  
import ElementUI from 'element-ui';  
import 'element-ui/lib/theme-chalk/index.css'; // 引入Element UI样式文件  
import showDialog from './d'; // 引入你封装的showDialog方法  
  
Vue.use(ElementUI); // 使用Element UI库  
Vue.prototype.$showDialog = showDialog; // 将showDialog方法挂载到Vue的原型上,以便在任何组件中都可以使用this.$showDialog()来调用它。

  export default {  
    methods:{
        a(){
              this.$showDialog({ title: '自定义标题', width: '30%' }); // 使用自定义的标题和宽度打开对话框。 
        }
    }
  }  
  </script>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值