增加自动icon功能,所有添加到web/assets/icons下的svg会自动被注册为icon组件使用【可用于menu和直接用标签加文件名使用】

This commit is contained in:
piexlMax
2023-12-10 23:30:39 +08:00
parent 8b9a75ba5a
commit 0f2018d913
6 changed files with 47 additions and 5 deletions
+2 -1
View File
@@ -5,7 +5,8 @@
const config = {
appName: 'Gin-Vue-Admin',
appLogo: 'https://www.gin-vue-admin.com/img/logo.png',
showViteLogo: true
showViteLogo: true,
logs: [],
}
export const viteLogo = (env) => {
+39
View File
@@ -1,13 +1,52 @@
import config from './config'
import { h } from 'vue'
// 统一导入el-icon图标
import * as ElIconModules from '@element-plus/icons-vue'
// 导入转换图标名称的函数
const createIconComponent = (svgContent) => ({
name: 'SvgIcon',
props: {
iconClass: {
type: String,
default: '',
},
className: {
type: String,
default: '',
},
},
render() {
const { className } = this
return h('span', {
class: className,
ariaHidden: true,
innerHTML: svgContent,
})
},
})
const registerIcons = async(app) => {
const iconModules = import.meta.glob('@/assets/icons/**/*.svg')
for (const path in iconModules) {
const response = await fetch(path)
const svgContent = await response.text()
const iconName = path.split('/').pop().replace(/\.svg$/, '')
const iconComponent = createIconComponent(svgContent)
config.logs.push({
'key': iconName,
'label': iconName,
})
app.component(iconName, iconComponent)
}
}
export const register = (app) => {
// 统一注册el-icon图标
for (const iconName in ElIconModules) {
app.component(iconName, ElIconModules[iconName])
}
registerIcons(app)
app.config.globalProperties.$GIN_VUE_ADMIN = config
}