Element-Plus 图标自动导入

🚀 作者主页: 有来技术

🔥 开源项目: youlai-mall 🍃 vue3-element-admin 🍃 youlai-boot

🌺 仓库主页: Gitee 💫 Github 💫 GitCode

💖 欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请纠正!

目录

引言

Element-Plus 官方提供了四种 安装图标方式方式,本文将详细介绍如何通过自动导入方式。从 Iconify 中自动导入任何图标集。

本篇参考官方 自动导入模板。

安装 Element-Plus

bash 复制代码
npm install element-plus

安装自动导入依赖

Element-Plus 自动导入(推荐)

bash 复制代码
npm install -D unplugin-auto-import unplugin-vue-components 

安装自动导入图标依赖

Element-Plus 图标自动导入

bash 复制代码
npm i -D unplugin-icons

自动导入配置

.eslintrc.cjs

自动导入函数 eslint 规则引入

json 复制代码
"extends": [
    "./.eslintrc-auto-import.json"
]

tsconfig.json

自动导入TS类型声明文件引入

json 复制代码
{
  "include": ["src/**/*.d.ts"]
}

vite.config.ts

typescript 复制代码
import path from 'path'
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

const pathSrc = path.resolve(__dirname, 'src')

export default defineConfig({
  resolve: {
    alias: {
      '@': pathSrc,
    },
  },
  plugins: [
    Vue({
      reactivityTransform: true,
    }),
    AutoImport({
      // Auto import functions from Vue, e.g. ref, reactive, toRef...
      // 自动导入 Vue 相关函数,如:ref, reactive, toRef 等
      imports: ['vue', '@vueuse/core'],
      dirs: [path.resolve(pathSrc, 'composables')],

      // Auto import functions from Element Plus, e.g. ElMessage, ElMessageBox... (with style)
      // 自动导入 Element Plus 相关函数,如:ElMessage, ElMessageBox... (带样式)
      resolvers: [
        ElementPlusResolver(),

        // Auto import icon components
        // 自动导入图标组件
        IconsResolver({}),
      ],
      vueTemplate: true,
      dts: path.resolve(pathSrc, 'typings', 'auto-imports.d.ts'),
    }),

    Components({
      resolvers: [
        // Auto register icon components
        // 自动注册图标组件
        IconsResolver({
          enabledCollections: ['ep'],
        }),
        // Auto register Element Plus components
        // 自动导入 Element Plus 组件
        ElementPlusResolver(),
      ],

      dts: path.resolve(pathSrc, 'typings', 'components.d.ts'),
    }),

    Icons({
      autoInstall: true,
    }),
  ],
})

自动导入图标使用

默认名称格式: i-ep-图标名 ,图标名在Element-Plus 官方-图标集合 查询

html 复制代码
	<div class="avatar">
	  <img :src="userStore.user.avatar + '?imageView2/1/w/80/h/80'" />
	  <!-- i-ep-图标名(CaretBottom ) -->
	  <i-ep-CaretBottom />
	</div>

效果如下:

自动导入图标样式

如何修改通过自动导入图标的大小和颜色样式?

html 复制代码
 <el-icon :size="12" color="#409eff">
   <i-ep-CaretBottom />
 </el-icon>

开源项目

  • 微服务商城项目
Github Gitee
开源组织 有来开源组织 有来开源组织
后端 youlai-mall 📖 youlai-mall 📖
前端 mall-admin🌎 mall-admin 🌎
移动端 mall-app 🌎 mall-app 🌎
  • 前后端分离项目
Github Gitee
开源组织 有来开源组织 有来开源组织
后端 youlai-boot 📖 youlai-boot 📖
前端 vue3-element-admin 🌎 vue3-element-admin 🌎
相关推荐
上海懒图科技1 天前
工厂厂区电子地图定位导航:厂区人车精准定位,安全合规双保障
前端·arcgis·vue
卋罖7505 天前
Razor 视图 + JavaScript + Vue/iView 组件实现的表格自定义列
javascript·vue·iview·razor视图
无所谓^_^8 天前
个人健康管理系统 | Springboot3 | Vue3
java·vue·毕业设计·springboot·课程设计·毕设·前后端分离
在努力的前端小白8 天前
基于FastAPI+LangChain+LangGraph+Agent+多模态的AI Agent 有渔渔具网站
人工智能·langchain·vue·fastapi
sun_weitao8 天前
用 FastAPI + MQTT 做一个 ESP32最小化跑通示例
中间件·vue·fastapi
小大宇11 天前
Vue知识点-ElementPlus-06 单选+多选+下拉+日期选择
前端·vue.js·elementui
汤米粥12 天前
2026年主流前端技术框架
vue·react·elementplus·tailwind css·angular‌·svelte‌·bootstrap‌
切糕师学AI12 天前
el-select-v2 深度解析:用虚拟滚动解决海量数据下拉选择性能瓶颈
前端·vue.js·elementui·el-select-v2
codigger13 天前
Vue nextTick 到底在等什么?一篇讲透原理与那些年踩过的坑
程序员·vue·编程·异步更新·#人工智能
咚咚咚小柒13 天前
【前端】LangChain.js学习
前端·javascript·人工智能·typescript·langchain·vue·ai编程