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