♻️(component): [teleportWrapper]使用 mutation observer,确保即使在渲染目标元素前挂载了 teleportWrapper, 他也能正常工作
This commit is contained in:
parent
77ff852aaa
commit
b826124c92
@ -19,15 +19,22 @@ const props = withDefaults(defineProps<TeleportWrapperProps>(), {
|
||||
const target = ref<Element | null>(null);
|
||||
|
||||
onMounted(() => {
|
||||
if (!props.to) return;
|
||||
const observer = new MutationObserver((mutationList, observer) => {
|
||||
for (const mutation of mutationList) {
|
||||
if (mutation.type !== "childList") continue;
|
||||
const el = document.querySelector(props.to);
|
||||
if (el) {
|
||||
if (!el) continue;
|
||||
target.value = el;
|
||||
observer.disconnect();
|
||||
break;
|
||||
}
|
||||
});
|
||||
observer.observe(document, { childList: true, subtree: true });
|
||||
return () => observer.disconnect();
|
||||
});
|
||||
</script>
|
||||
<template>
|
||||
<Teleport :to="target" :disabled="!target || disabled">
|
||||
<Teleport :to="target" v-if="target" :disabled="!target || disabled">
|
||||
<slot></slot>
|
||||
</Teleport>
|
||||
</template>
|
||||
|
@ -1,12 +1,12 @@
|
||||
import { App, Plugin } from "vue";
|
||||
import { App, Component, Plugin } from "vue";
|
||||
|
||||
export type WithInstallType<T> = T & Plugin;
|
||||
|
||||
export const withInstall = <T>(comp: T): T & Plugin => {
|
||||
const component = comp as any;
|
||||
const component = comp as T & Component & Plugin;
|
||||
|
||||
component.install = (app: App) => {
|
||||
app.component(component.name, comp);
|
||||
app.component(component.name!, comp as T & Component & Plugin);
|
||||
};
|
||||
|
||||
return component as T & Plugin;
|
||||
|
Loading…
Reference in New Issue
Block a user