首先需要安装unplugin-vue-components,unplugin-auto-import,unplugin-icons三款插件。
npm install -D unplugin-vue-components unplugin-auto-import unplugin-icons
然后vite.config.js里加入下面的配置:
import { defineConfig } from "vite";
import Vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import Icons from "unplugin-icons/vite";
import IconsResolver from "unplugin-icons/resolver";
export default defineConfig({
// ...
plugins: [
Vue(),
AutoImport({
imports: ['vue'],
resolvers: [
ElementPlusResolver(),
IconsResolver({
prefix: "Icon",
}),
],
}),
Components({
resolvers: [
IconsResolver({
enabledCollections: ["ep"],
}),
ElementPlusResolver(),
],
directoryAsNamespace: true,
}),
Icons({
autoInstall: true,
compiler: 'vue3'
}),
],
// ...
});
官方还有个ts版最佳实践:
另外有个官方文档没提及的地方,用这种方式自动引入的图标写法要稍微改变,文档用法是:
<el-icon><Link /></el-icon>
这样图标不会显示出来,尝试后发现要改成:
<el-icon><i-ep-Link /></el-icon>
前面要加上i-ep-前缀,文档没提示这点,猜测是因为这里的配置
标签:vue,icons,element,unplugin,plus,components,Vue3,import,vite From: https://blog.csdn.net/qq_34718221/article/details/136624503