componentMap.ts
1.1 KB
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
45
import { tryOnUnmounted } from '@vueuse/core';
import { Component } from 'vue';
/**
 * @description 转换前端组件key, 兼容旧数据
 * @param string
 */
export const transformComponentKey = (string: string) => {
  const CONNECTION_SYMBOL = '-';
  const needTransformFlag = (string || '').includes(CONNECTION_SYMBOL);
  if (needTransformFlag) {
    return string
      .split(CONNECTION_SYMBOL)
      .map((item) => `${item.substring(0, 1).toUpperCase()}${item.substring(1).toLowerCase()}`)
      .join('');
  }
  return string;
};
export const componentMap = new Map();
export const registerComponent = (name: string, component: Component) => {
  const _name = transformComponentKey(name);
  if (componentMap.has(_name)) {
    return componentMap.get(_name);
  }
  componentMap.set(_name, component);
  tryOnUnmounted(() => {
    uninstallComponent(_name);
  });
  return component;
};
export const uninstallComponent = (name: string) => {
  componentMap.delete(transformComponentKey(name));
};
export const getComponent = (frontId: string) => {
  return componentMap.get(transformComponentKey(frontId));
};