global.js 1.4 KB

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