wupengfei
2025-02-12 2d4a6ebe75599eaa51d5fb0f522dc9fa6825af00
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
39
40
41
42
43
44
<script lang="ts">
import { State, PortalNode } from './portal';
 
export default defineComponent({
  name: 'PortalManager',
  setup(props, { expose }) {
    const state = reactive<State>({
      portals: [],
    });
 
    const mount = (key: number, children: PortalNode) => {
      state.portals.push({ key, children });
    };
    const update = (key: number, children: PortalNode) => {
      state.portals = state.portals.map((item) => {
        if (item.key === key) {
          return { ...item, children };
        }
        return item;
      });
    };
 
    const unmount = (key: number) => {
      state.portals = state.portals.filter((item) => item.key !== key);
    };
 
    expose({
      mount,
      update,
      unmount,
    });
 
    return () => {
      return h(
        'div',
        null,
        state.portals.map((item) => {
          return h('div', { key: item.key }, [item.children]);
        })
      );
    };
  },
});
</script>