index.js 1.14 KB
/*
 * @Description: 
 * @Autor: renchao
 * @LastEditTime: 2023-06-14 15:05:38
 */
import Vue from 'vue'
import Popup from './index.vue'
const PopupBox = Vue.extend(Popup)
let popuping = undefined

PopupBox.prototype.close = function () {
  // 如果Popup 有引用,则去掉引用
  if (popuping) {
    popuping = undefined
  }
  // 先将组件隐藏
  this.isShow = false
  // 延迟300毫秒,等待Popup关闭动画执行完之后销毁组件
  setTimeout(() => {
    // 移除挂载的dom元素
    if (this.$el && this.$el.parentNode) {
      this.$el.parentNode.removeChild(this.$el)
    }
  }, 300)
}

const Popup1 = (title, editItem, data, formData) => {
  // 如果组件已渲染,则返回即可
  if (popuping) {
    return popuping
  }
  data.title = title
  data.editItem = editItem
  if (formData) {
    data.formData = formData
  }
  // 通过构造函数初始化组件 相当于 new Vue()
  let instance = new PopupBox({
    data
  }).$mount()
  document.body.appendChild(instance.$el)
  Vue.nextTick(() => {
    instance.isShow = true
    // 将组件实例赋值给loading
    popuping = instance
  })
  return instance
}
export default Popup1