fuliqi
2024-01-24 29c1e7eb5ac16e90d8991a86c1c071bc312ec8d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import alertEl from './index.vue'
import Vue from 'vue'
const Alert = Vue.extend(alertEl)
const alertFnc = (msg, title, callback) => {
  // 实例化弹窗子类
  var alertVm = new Alert({
    propsData: {
      isShow: true,
      title: title
    }
  })
 
  // 指定弹窗默认插槽的内容
  alertVm.$slots.default = [msg]
 
  var alertEl = document.createElement('div')
  document.body.appendChild(alertEl)
  alertVm.$mount(alertEl) // 挂载到元素
 
  // 定义关闭事件,在弹窗子类里面触发
  alertVm.$on('close', () => {
    alertVm.isShow = false
 
    alertVm.$nextTick(() => {
      alertVm.$destroy()
      alertEl.remove()
    })
 
    if (callback) callback()
  })
}
var alertPlugin = {}
alertPlugin.install = function (Vue, options) {
  Vue.prototype.$alert = function (msg, title, callback) {
    alertFnc(msg, title, callback)
  }
}
export default alertPlugin