global.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. if (iconName.indexOf(' ') !== -1) {
  21. console.error(`icon ${iconName}.svg includes whitespace`)
  22. continue
  23. }
  24. const iconComponent = createIconComponent(iconName)
  25. config.logs.push({
  26. 'key': iconName,
  27. 'label': iconName,
  28. })
  29. app.component(iconName, iconComponent)
  30. }
  31. }
  32. export const register = (app) => {
  33. // 统一注册el-icon图标
  34. for (const iconName in ElIconModules) {
  35. app.component(iconName, ElIconModules[iconName])
  36. }
  37. app.component('SvgIcon', svgIcon)
  38. registerIcons(app)
  39. app.config.globalProperties.$GIN_VUE_ADMIN = config
  40. }