fix(Loading): 处理v-loading指令和useLoading的内存泄露 (#3861)
Co-authored-by: jinchuanwang <1085296443@qq.com>
This commit is contained in:
parent
144cdd4680
commit
bcd98ee067
|
|
@ -19,13 +19,14 @@ export function createLoading(props?: Partial<LoadingProps>, target?: HTMLElemen
|
||||||
|
|
||||||
vm = createVNode(LoadingWrap);
|
vm = createVNode(LoadingWrap);
|
||||||
|
|
||||||
|
let container: Nullable<HTMLElement> = null;
|
||||||
if (wait) {
|
if (wait) {
|
||||||
// TODO fix https://github.com/anncwb/vue-vben-admin/issues/438
|
// TODO fix https://github.com/anncwb/vue-vben-admin/issues/438
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
render(vm, document.createElement('div'));
|
container && render(vm, (container = document.createElement('div')));
|
||||||
}, 0);
|
}, 0);
|
||||||
} else {
|
} else {
|
||||||
render(vm, document.createElement('div'));
|
render(vm, (container = document.createElement('div')));
|
||||||
}
|
}
|
||||||
|
|
||||||
function close() {
|
function close() {
|
||||||
|
|
@ -41,6 +42,11 @@ export function createLoading(props?: Partial<LoadingProps>, target?: HTMLElemen
|
||||||
target.appendChild(vm.el as HTMLElement);
|
target.appendChild(vm.el as HTMLElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function destory() {
|
||||||
|
container && render(null, container);
|
||||||
|
container = vm = null;
|
||||||
|
}
|
||||||
|
|
||||||
if (target) {
|
if (target) {
|
||||||
open(target);
|
open(target);
|
||||||
}
|
}
|
||||||
|
|
@ -48,6 +54,7 @@ export function createLoading(props?: Partial<LoadingProps>, target?: HTMLElemen
|
||||||
vm,
|
vm,
|
||||||
close,
|
close,
|
||||||
open,
|
open,
|
||||||
|
destory,
|
||||||
setTip: (tip: string) => {
|
setTip: (tip: string) => {
|
||||||
data.tip = tip;
|
data.tip = tip;
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
import { unref } from 'vue';
|
import { unref } from 'vue';
|
||||||
|
import type { Ref } from 'vue';
|
||||||
|
import { tryOnUnmounted } from '@vueuse/core';
|
||||||
import { createLoading } from './createLoading';
|
import { createLoading } from './createLoading';
|
||||||
import type { LoadingProps } from './typing';
|
import type { LoadingProps } from './typing';
|
||||||
import type { Ref } from 'vue';
|
|
||||||
|
|
||||||
export interface UseLoadingOptions {
|
export interface UseLoadingOptions {
|
||||||
target?: any;
|
target?: any;
|
||||||
|
|
@ -45,5 +46,9 @@ export function useLoading(
|
||||||
instance.setTip(tip);
|
instance.setTip(tip);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
tryOnUnmounted(() => {
|
||||||
|
instance.destory();
|
||||||
|
});
|
||||||
|
|
||||||
return [open, close, setTip];
|
return [open, close, setTip];
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ const loadingDirective: Directive = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
unmounted(el) {
|
unmounted(el) {
|
||||||
el?.instance?.close();
|
el?.instance?.destory();
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue