安装依赖
npm install @element-plus/icons-vue
全局注册
main.ts代码如下
import { createApp } from "vue";
import App from "./App.vue";
import "element-plus/lib/theme-chalk/el-message.css";
import * as ElIcons from "@element-plus/icons";
const app = createApp(App);
for (const name in ElIcons) {
app.component(name, (ElIcons as any)[name]);
}
app.mount("#app");
代码中使用
1.结合按钮使用
<el-button type="primary" icon="Edit" >编辑</el-button>
<el-button
size="mini"
type="primary"
class="inline-block"
icon="More"
title="详情"
@click="handleDetail(row.id)"
/>
2.结合el-icon使用
<el-icon>
<delete />
</el-icon>
3.使用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="route.meta.icon" />
</el-icon>
<span>{{ route.meta.title }}</span>
</el-menu-item>
<el-sub-menu v-else :index="route.path">
<template #title>
<el-icon>
<component :is="route.meta.icon" />
</el-icon>
<span>{{ route.meta.title }}</span>
</template>
<el-menu-item
v-for="subRoute in route.children"
:key="subRoute.path"
:index="subRoute.path"
>
<el-icon>
<component :is="subRoute.meta.icon" />
</el-icon>
<span>{{ subRoute.meta.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>
- 表单中 prefix ,suffix 前置,后置图标使用
<el-form-item class="input-button">
<div class="input">
<el-input prefix-icon="Lock" suffix-icon="View" type="password" v-model="loginForm.password"
autocomplete="off" placeholder="请输入密码">
<template #prefix>
<lock></lock>
</template>
<template #suffix>
<View></View>
</template>
</el-input>
</div>
</el-form-item>
局部引入,需要哪个图标引入哪个,因为element-plus中每个图标都是一个组件来的。如:
import { User, Lock, View } from "@element-plus/icons-vue";