Vue3中element-plus全局使⽤Icon图标的过程详解⽬录
1、安装图标库
2、注册
3、在组件中直接使⽤
4、在el-menu组件中动态使⽤
总结
Vue项⽬中使⽤Element-plus的Icon图标,包括按钮和动态菜单
1、安装图标库
npm install @element-plus/icons
2、注册
main.ts⽂件中引⼊并注册
import { createApp } from 'vue'
import App from './App.vue'
import ElementPlus from 'element-plus'
import * as ElIcons from '@element-plus/icons'
const app = createApp(App)
for (const name in ElIcons){
appponent(name,(ElIcons as any)[name])
}
app.use(ElementPlus).mount('#app')
3、在组件中直接使⽤
// 结合按钮使⽤
<el-button type="primary" icon="Edit" >编辑</el-button>
<el-button
size="mini"
type="primary"
class="inline-block"
icon="More"
title="详情"
@click="handleDetail(row.id)"
/>
// 结合el-icon使⽤
<el-icon>
<delete />
</el-icon>
4、在el-menu组件中动态使⽤
如果想在渲染菜单时动态使⽤icon图标,需要使⽤动态组件,举个栗⼦:
// 路由⽂件
export const routes: Array<RouteRecordRaw> = [
{
path: '/',
component: Layout,
redirect: 'home',
children: [
{
path: '/home',
component: () => import('@/views/Home.vue'),
vue element adminname: 'Home',
meta: { title: '⾸页', icon: 'HomeFilled' },
},
],
},
]
// 使⽤el-menu的⽂件
<template>
<div>
<el-scrollbar wrap-class="scrollbar-wrapper">
<el-menu
router
:default-active="activeMenu"
@open="handleOpen"
@close="handleClose"
>
<template v-for="route in menuList" :key="route.path">
<el-menu-item v-if="!route.children" :index="route.path">
<el-icon>
<component :is="a.icon" />
</el-icon>
<span>{{ a.title }}</span>
</el-menu-item>
<el-sub-menu v-else :index="route.path">
<template #title>
<el-icon>
<component :is="a.icon" />
</el-icon>
<span>{{ a.title }}</span>
</template>
<el-menu-item
v-for="subRoute in route.children"
:key="subRoute.path"
:index="subRoute.path"
>
<el-icon>
<component :is="a.icon" />
</el-icon>
<span>{{ a.title }}</span>
</el-menu-item>
</el-sub-menu>
</template>
</el-menu>
</el-scrollbar>
</div>
</template>
<script lang="ts">
import { defineComponent, computed } from 'vue'
import { useRoute } from 'vue-router'
import { routes } from '@/router/index'
export default defineComponent({
setup() {
const route = useRoute()
const activeMenu = computed(() => {
const { meta, path } = route
if (meta.activeMenu) {
return meta.activeMenu
}
return path
})
const menuList = computed(
() => (routes as any).find((item: any) => item.path === '/').children
)
return { activeMenu, menuList }
},
})
</script>
总结
到此这篇关于Vue3中element-plus全局使⽤Icon图标的⽂章就介绍到这了,更多相关Vue3 element-plus全局使⽤Icon图标内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论