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

View File

@ -1,14 +1,14 @@
<template> <template>
<el-menu :default-active="activeMenu" mode="horizontal" @select="handleSelect" :ellipsis="false"> <el-menu :default-active="activeMenu" mode="horizontal" @select="handleSelect" :ellipsis="false">
<template v-for="(item, index) in topMenus"> <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" /> <svg-icon :name="item.meta.icon" />
&nbsp;{{ item.meta.title }} &nbsp;{{ item.meta.title }}
</el-menu-item> </el-menu-item>
</template> </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 #title>更多菜单</template>
<template v-for="(item, index) in topMenus"> <template v-for="(item, index) in topMenus">
<el-menu-item :index="item.path" :key="index" v-if="index >= visibleNumber"> <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 router.children[item].path = '/redirect/' + router.children[item].path
} else { } else {
if (!isHttp(router.children[item].path)) { if (!isHttp(router.children[item].path)) {
router.children[item].path = router.children[item].path = router.path + '/' + router.children[item].path
router.path + '/' + router.children[item].path
} }
} }
router.children[item].parentPath = router.path router.children[item].parentPath = router.path
@ -125,7 +124,7 @@ function handleSelect(key, keyPath) {
window.open(key, '_blank') window.open(key, '_blank')
} else if (key.indexOf('/redirect') !== -1) { } else if (key.indexOf('/redirect') !== -1) {
// /redirect // /redirect
router.push({ path: key.replace("/redirect", "") }).catch(err => {}); router.push({ path: key.replace('/redirect', '') }).catch((err) => {})
} else { } else {
// //
activeRoutes(key) activeRoutes(key)