增加自动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
+1
View File
@@ -0,0 +1 @@
<svg t="1702221805491" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5932"><path d="M876.48 981.312H147.52a104.832 104.832 0 0 1-104.832-104.832V469.76a46.08 46.08 0 0 1 46.912-46.912 46.08 46.08 0 0 1 46.912 46.912v406.72c0 6.272 4.736 11.008 10.944 11.008h729.088a10.688 10.688 0 0 0 10.88-11.008V469.76a46.08 46.08 0 0 1 46.976-46.912 46.08 46.08 0 0 1 46.912 46.912v406.72c0 57.92-46.912 104.832-104.832 104.832z" p-id="5933"></path><path d="M934.4 516.672a49.664 49.664 0 0 1-31.296-12.48L512 152.192l-391.104 352a46.784 46.784 0 0 1-65.728-3.2 48.64 48.64 0 0 1 3.2-67.2l422.4-378.624a47.616 47.616 0 0 1 62.528 0l422.4 378.624a46.784 46.784 0 0 1 3.136 65.664 46.08 46.08 0 0 1-34.432 17.28z" p-id="5934"></path><path d="M627.776 981.312H396.16a46.08 46.08 0 0 1-46.912-46.912V558.912A46.08 46.08 0 0 1 396.16 512h229.952a46.08 46.08 0 0 1 46.976 46.912V934.4a44.8 44.8 0 0 1-45.44 46.912z m-184.64-93.824h136.128v-281.6H443.136v281.6z" p-id="5935"></path></svg>

After

Width:  |  Height:  |  Size: 1017 B

+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
}
+4 -1
View File
@@ -40,6 +40,7 @@
<script setup>
import { ref, reactive } from 'vue'
import config from "@/core/config";
defineOptions({
name: 'Icon',
@@ -1175,12 +1176,14 @@ const options = reactive([
'key': 'wind-power',
'label': 'wind-power',
},
...config.logs
])
console.log(config.logs)
const metaData = ref(props.meta)
if (!metaData.value.icon) {
metaData.value.icon = options[0].label
}
</script>
<style lang="scss">