1.package.json
"dependencies": {
"@element-plus/icons-vue": "^2.1.0",
"element-plus": "^2.3.7",
},
"devDependencies": {
"@iconify-json/ep": "^1.1.11",
"unplugin-auto-import": "^0.16.4",
"unplugin-icons": "^0.16.3",
"unplugin-vue-components": "^0.25.1",
}
2.vite.config.js
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite' // 自动导入
import Components from 'unplugin-vue-components/vite' // 组件注册
import { ElementPlusResolver, NaiveUiResolver } from 'unplugin-vue-components/resolvers'
// 自动导入element图标
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { resolve } from 'path'
export default defineConfig(({ command, mode }) => {
const env = loadEnv(mode, process.cwd(), '')
return {
base: './',
plugins: [
vue(),
AutoImport({
// 自动导入 Vue 相关函数,如:ref, reactive, toRef 等
imports: ['vue'],
resolvers: [
// 自动导入element plus相关函数(带样式)
ElementPlusResolver(),
// 自动导入图标组件
IconsResolver({
prefix: 'Icon',
enabledCollections: ['ep']
})
]
}),
Components({
// 要搜索组件的目录的相对路径。默认 ['src/components']
dirs: ['src'],
// 组件的有效文件扩展名。
extensions: ['vue'],
// 搜索子目录
deep: true,
resolvers: [
// 自动导入element plus组件
ElementPlusResolver(),
NaiveUiResolver(),
// 自动注册图标组件
IconsResolver({
prefix: 'i',
enabledCollections: ['ep']
})
]
}),
Icons({
compiler: 'vue3',
autoInstall: true
})
],
resolve: {
// 配置路径别名
alias: {
'@': resolve(__dirname, './src'),
'api': resolve(__dirname, './src/api'),
'views': resolve(__dirname, './src/views'),
'utils': resolve(__dirname, './src/utils'),
'comp': resolve(__dirname, './src/components'),
'assets': resolve(__dirname, './src/assets')
},
extensions: ['.js', '.ts', '.vue', '.json', '.less', '.css']
},
define: {
'process.env': {
VITE_APP_BASE_API: env.VITE_APP_BASE_API
}
},
server: {
host: '0.0.0.0',
port: 3000, // 端口
open: false, // 启动项目后打开浏览器
hot: true,
overlay: {
warning: false,
error: true
},
proxy: {
[env.VITE_APP_BASE_API]: {
target: 'https://devestate.yyepark.com/',
ws: true,
changeOrigin: true,
secure: false,
rewrite: (path) => path.replace('^' + [env.VITE_APP_BASE_API], '')
}
}
}
}
})
3.使用图标前面加上:<i-ep-xxx />
<el-icon><i-ep-CloseBold /></el-icon>
4.路由中使用
<el-icon v-if="childItem.meta && childItem.meta.icon">
<component :is="childItem.meta.icon" />
</el-icon>
import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '../layout/index.vue'
import { markRaw } from 'vue'文章来源:https://www.toymoban.com/news/detail-612664.htmlexport const asyncRoutes = [
{
path: '/',
name: 'HomePage',
component: Layout,
redirect: '/home',
children: [
{
path: 'home',
name: 'Home',
component: () => import('@/views/Home/index.vue'),
meta: { title: '首页', icon: markRaw(IconEpMenu) } //markRaw
}
]
}
]文章来源地址https://www.toymoban.com/news/detail-612664.html
到了这里,关于vue3+vite路由中使用element自动导入图标的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!