Icon icons using Element-plus in Vue project, including buttons and dynamic menus
1. Install the icon library
npm install @element-plus/icons
2. Register
Introduce and register in the file
import { createApp } from 'vue' import App from './' import ElementPlus from 'element-plus' import * as ElIcons from '@element-plus/icons' const app = createApp(App) for (const name in ElIcons){ (name,(ElIcons as any)[name]) } (ElementPlus).mount('#app')
3. Use directly in components
// Use with buttons<el-button type="primary" icon="Edit" >edit</el-button> <el-button size="mini" type="primary" class="inline-block" icon="More" title="Details" @click="handleDetail()" /> // Use in combination with el-icon<el-icon> <delete /> </el-icon>
4. Dynamic use in el-menu components
If you want to use the icon icon dynamically when rendering the menu, you need to use a dynamic component, give me a chestnut:
//Route fileexport const routes: Array<RouteRecordRaw> = [ { path: '/', component: Layout, redirect: 'home', children: [ { path: '/home', component: () => import('@/views/'), name: 'Home', meta: { title: 'front page', icon: 'HomeFilled' }, }, ], }, ]
// Files using 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=""> <el-menu-item v-if="!" :index=""> <el-icon> <component :is="" /> </el-icon> <span>{{ }}</span> </el-menu-item> <el-sub-menu v-else :index=""> <template #title> <el-icon> <component :is="" /> </el-icon> <span>{{ }}</span> </template> <el-menu-item v-for="subRoute in " :key="" :index="" > <el-icon> <component :is="" /> </el-icon> <span>{{ }}</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 () { return } return path }) const menuList = computed( () => (routes as any).find((item: any) => === '/').children ) return { activeMenu, menuList } }, }) </script>
Summarize
This is the article about the global use of Icon icons in Vue3. For more information about Vue3 element-plus using Icon icons, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!