global.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import config from './config'
  2. import { h } from 'vue'
  3. // 统一导入el-icon图标
  4. import * as ElIconModules from '@element-plus/icons-vue'
  5. import svgIcon from '@/components/svgIcon/svgIcon.vue'
  6. // 导入转换图标名称的函数
  7. const createIconComponent = (name) => ({
  8. name: 'SvgIcon',
  9. render() {
  10. return h(svgIcon, {
  11. name: name,
  12. })
  13. },
  14. })
  15. const registerIcons = async(app) => {
  16. const iconModules = import.meta.glob('@/assets/icons/**/*.svg')
  17. for (const path in iconModules) {
  18. const iconName = path.split('/').pop().replace(/\.svg$/, '')
  19. // 如果iconName带空格则不加入到图标库中并且提示名称不合法
  20. console.log(iconName)
  21. if (iconName.indexOf(' ') !== -1) {
  22. console.error(`icon ${iconName}.svg includes whitespace`)
  23. continue
  24. }
  25. const iconComponent = createIconComponent(iconName)
  26. config.logs.push({
  27. 'key': iconName,
  28. 'label': iconName,
  29. })
  30. app.component(iconName, iconComponent)
  31. }
  32. }
  33. export const register = (app) => {
  34. // 统一注册el-icon图标
  35. for (const iconName in ElIconModules) {
  36. app.component(iconName, ElIconModules[iconName])
  37. }
  38. app.component('SvgIcon', svgIcon)
  39. registerIcons(app)
  40. app.config.globalProperties.$GIN_VUE_ADMIN = config
  41. }