[bugfix] Dialog: can't show after being removed from document (#3111)

This commit is contained in:
neverland 2019-04-09 13:06:07 +08:00 committed by GitHub
parent fc412ce1aa
commit 2830f4cacd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,11 @@ import { isServer } from '../utils';
let instance; let instance;
const initInstance = () => { function initInstance() {
if (instance) {
instance.$destroy();
}
instance = new (Vue.extend(VanDialog))({ instance = new (Vue.extend(VanDialog))({
el: document.createElement('div'), el: document.createElement('div'),
// avoid missing animation when first rendered // avoid missing animation when first rendered
@ -16,16 +20,16 @@ const initInstance = () => {
instance.$on('input', value => { instance.$on('input', value => {
instance.value = value; instance.value = value;
}); });
}; }
const Dialog = options => { function Dialog(options) {
/* istanbul ignore if */ /* istanbul ignore if */
if (isServer) { if (isServer) {
return Promise.resolve(); return Promise.resolve();
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!instance) { if (!instance || !document.body.contains(instance.$el)) {
initInstance(); initInstance();
} }
@ -34,7 +38,7 @@ const Dialog = options => {
reject reject
}); });
}); });
}; }
Dialog.defaultOptions = { Dialog.defaultOptions = {
value: true, value: true,
@ -79,11 +83,12 @@ Dialog.resetDefaultOptions = () => {
Dialog.currentOptions = { ...Dialog.defaultOptions }; Dialog.currentOptions = { ...Dialog.defaultOptions };
}; };
Dialog.resetDefaultOptions();
Dialog.install = () => { Dialog.install = () => {
Vue.use(VanDialog); Vue.use(VanDialog);
}; };
Vue.prototype.$dialog = Dialog; Vue.prototype.$dialog = Dialog;
Dialog.resetDefaultOptions();
export default Dialog; export default Dialog;