component-map.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import type { Component } from 'vue';
  2. import type { CustomComponentType } from './types';
  3. import { getFileNameWithoutExtension, toPascalCase } from '#/utils';
  4. const componentMap = new Map<CustomComponentType | string, Component>();
  5. // import.meta.glob() 直接引入所有的模块 Vite 独有的功能
  6. const modules = import.meta.glob('./components/**/*.vue', { eager: true });
  7. // 加入到路由集合中
  8. Object.keys(modules).forEach((key) => {
  9. if (!key.includes('-ignore')) {
  10. const mod = (modules as any)[key].default || {};
  11. const compName = getFileNameWithoutExtension(key);
  12. componentMap.set(toPascalCase(compName), mod);
  13. }
  14. });
  15. export function add(compName: string, component: Component) {
  16. componentMap.set(compName, component);
  17. }
  18. export function del(compName: string) {
  19. componentMap.delete(compName);
  20. }
  21. /**
  22. * 注册组件
  23. * @param components
  24. */
  25. export const registerComponent = (components: any) => {
  26. componentMap.forEach((value, key) => {
  27. components[key] = value as Component;
  28. });
  29. };
  30. export { componentMap };