initComponent.ts 941 B

1234567891011121314151617181920212223242526
  1. import { App, Component } from 'vue'
  2. import EasyMapComponent from '@/components/easyMap/index.vue'
  3. import EasyMapGLComponent from '@/components/easyMapGL/index.vue'
  4. interface FileType {
  5. [key: string]: Component
  6. }
  7. // @ts-ignore
  8. const Components: Record<string, FileType> = import.meta.globEager("/src/components/**/*.vue")
  9. export default (app: App): void => {
  10. // 因为通过 import.meta.globEager 返回的列表不能迭代所以直接使用 Object.keys 拿到 key 遍历
  11. Object.keys(Components).forEach((c: string) => {
  12. // const component = files[c]?.default
  13. const component = Components[c] ? Components[c].default : null
  14. // 组件内有注册过name才可自动挂载
  15. if (component && component.name) {
  16. // 挂载全局控件
  17. app.component(component.name as string, component)
  18. }
  19. })
  20. app.component('EasyMapComponent', EasyMapComponent)
  21. app.component('EasyMapGLComponent', EasyMapGLComponent)
  22. }