vue3 全局封装element-plus 的dialog

在对公司旧项目升级的时候,有一些vue2的代码对使用全局弹窗是通过this.$dialog(xxx)这种方式进行使用,那么今天我就介绍一些二次封装element-plus的dialog。不废话直接上代码。
src/components/Dialog/index.js

import Dialog from "./Dialog.vue";
import { h, render } from "vue";

let createMount = (opts) => {
  const mountNode = document.createElement("div");
  document.body.appendChild(mountNode);
  const vnode = h(Dialog, {
    ...opts,
    modelValue: true,
    remove() {
      document.body.removeChild(mountNode);
    },
  });
  vnode.appContext = Modal._context;
  render(vnode, mountNode);
};
const Modal = {
  install(app, options) {
    app.config.globalProperties.$dialog2 = {
      show: (title, component, options = {}, params) => {
        options.id = options.id || "v3popup_" + 1; //唯一id 删除组件时用于定位
        createMount({
          title,
          comps: component,
        });
      },
    };
  },
  _context: null,
};
export default Modal;

src/components/Dialog/Dialog.vue

<template>
  <el-dialog
    custom-class="subDialog"
    :title="title"
    v-model="dialogPopVisible"
    :width="width"
    :before-close="onBeforeClose"
    :center="true"
    v-bind="option"
  >
    <div><component :is="comps"></component></div>
  </el-dialog>
</template>

<script>
import { ElDialog } from "element-plus";
export default {
  data() {
    return {
      dialogPopVisible: true,
    };
  },
  components: { ElDialog },
  props: {
    title: {
      type: String,
      default: "",
    },
    width: {
      type: String,
      default: "550px",
    },
    option: {
      type: Object,
      default: () => {},
    },
    remove: {
      type: Function,
    },
    comps: {
      require: false,
    },
  },
  methods: {
    onBeforeClose(done) {
      done();
    },
  },
  watch: {
    dialogPopVisible(value) {
      if (!value) {
        this.remove();
      }
    },
  },
};
</script>

<style lang="scss">
.subDialog {
  &.el-dialog {
    z-index: 99;
    background-color: #ffffff;
    margin-top: 20% !important;
  }
  .el-dialog__header {
    width: 100%;
    height: 50px;
    line-height: 50px;
    box-sizing: border-box;
    padding: 0 25px;
    text-align: left;
    border-bottom: 1px solid rgba(0, 0, 0, 0.06);
  }
  .el-dialog__headerbtn {
    font-size: 20px;
    width: 40px;
    height: 40px;
  }
  .el-dialog__title {
    font-size: 18px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: rgba(0, 0, 0, 0.85);
  }
  .el-dialog__body {
    box-sizing: border-box;
    padding: 20px 25px;
  }
  .el-dialog__footer {
    text-align: right;
    height: 60px;
    line-height: 60px;
    padding: 0;
    margin-right: 20px;
    border-top: 1px solid rgba(0, 0, 0, 0.06);
  }
}
</style>

src/main.js

import { createApp } from "vue";
import App from "./App.vue";

import ElementPlus from "element-plus";
import "element-plus/dist/index.css";
import MDialog from "@/components/Dialog";

const app = createApp(App);

//注意这是重点
MDialog._context = app._context;
app.use(ElementPlus);
app.use(MDialog);
app.mount("#app");

然后就可以正常使用了,可以传递组件进来


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容