svg图标改用resolveComponent

This commit is contained in:
不做码农 2022-04-28 19:27:05 +08:00
parent a403c21924
commit 93c291a668
2 changed files with 32 additions and 33 deletions

View File

@ -1,13 +1,9 @@
<template>
<svg :class="svgClass" aria-hidden="true" v-if="iconType == 0">
<use :xlink:href="iconName" :fill="color" />
</svg>
<el-icon v-else><component :is="iconName" /></el-icon>
</template>
<script>
import { h, resolveComponent } from 'vue'
export default defineComponent({
props: {
// svg
name: {
type: String,
required: true,
@ -17,33 +13,37 @@ export default defineComponent({
type: String,
default: '',
},
// svg
color: {
type: String,
default: '',
},
},
setup(props) {
return {
iconType: computed(() => {
if (props.name.startsWith('ele')) {
return 1
if (props.name?.startsWith('ele')) {
return () =>
h(
'i',
{
class: 'el-icon',
},
[h(resolveComponent(props.name.replace('ele-', '')))],
)
} else {
return 0
}
}),
iconName: computed(() => {
if (props.name?.startsWith('ele-')) {
return props.name.replace('ele-', '')
} else {
return `#icon-${props.name}`
}
}),
svgClass: computed(() => {
if (props.className) {
return `svg-icon ${props.className}`
}
return 'svg-icon'
return () =>
h(
'svg',
{
name: props.name,
'aria-hidden': true,
style: `color: ${props.color}`,
class: 'svg-icon',
},
h('use', {
'xlink:href': `#icon-${props.name}`,
fill: `${props.color}`,
}),
)
}
},
})

View File

@ -1,14 +1,14 @@
<template>
<el-menu :default-active="activeMenu" mode="horizontal" @select="handleSelect" :ellipsis="false">
<template v-for="(item, index) in topMenus">
<el-menu-item :style="{'--theme': theme}" :index="item.path" :key="index" v-if="index < visibleNumber">
<el-menu-item :style="{ '--theme': theme }" :index="item.path" :key="index" v-if="index < visibleNumber">
<svg-icon :name="item.meta.icon" />
&nbsp;{{ item.meta.title }}
</el-menu-item>
</template>
<!-- 顶部菜单超出数量折叠 -->
<el-sub-menu :style="{'--theme': theme}" index="more" v-if="topMenus.length > visibleNumber">
<el-sub-menu :style="{ '--theme': theme }" index="more" v-if="topMenus.length > visibleNumber">
<template #title>更多菜单</template>
<template v-for="(item, index) in topMenus">
<el-menu-item :index="item.path" :key="index" v-if="index >= visibleNumber">
@ -67,8 +67,7 @@ const childrenMenus = computed(() => {
router.children[item].path = '/redirect/' + router.children[item].path
} else {
if (!isHttp(router.children[item].path)) {
router.children[item].path =
router.path + '/' + router.children[item].path
router.children[item].path = router.path + '/' + router.children[item].path
}
}
router.children[item].parentPath = router.path
@ -125,7 +124,7 @@ function handleSelect(key, keyPath) {
window.open(key, '_blank')
} else if (key.indexOf('/redirect') !== -1) {
// /redirect
router.push({ path: key.replace("/redirect", "") }).catch(err => {});
router.push({ path: key.replace('/redirect', '') }).catch((err) => {})
} else {
//
activeRoutes(key)