Blame view

src/components/Popup/index.js 1.06 KB
任超 committed
1 2 3
import Vue from 'vue'
import Popup from './index.vue'
const PopupBox = Vue.extend(Popup)
任超 committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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
  }
任超 committed
27 28 29 30 31
  data.title = title
  data.editItem = editItem
  if (formData) {
    data.formData = formData
  }
任超 committed
32
  // 通过构造函数初始化组件 相当于 new Vue()
任超 committed
33 34 35 36 37 38 39
  let instance = new PopupBox({
    data
  }).$mount()
  document.body.appendChild(instance.$el)
  Vue.nextTick(() => {
    instance.isShow = true
  })
任超 committed
40 41 42
  // 将组件实例赋值给loading
  popuping = instance
  return instance
任超 committed
43
}
任超 committed
44
export default Popup1