Vue
开发技巧、常用组件与插件的总结与实践
按需自动导入组件与图标
安装插件
安装
unplugin-vue-components
和unplugin-auto-import
这两款插件shpnpm add -D unplugin-vue-components unplugin-auto-import unplugin-icons
在
vite.config.ts
中添加以下配置tsimport {
resolve} from 'node:path' importAutoImportfrom 'unplugin-auto-import/vite' import {FileSystemIconLoader} from 'unplugin-icons/loaders' importIconsResolverfrom 'unplugin-icons/resolver' importIconsfrom 'unplugin-icons/vite' import {ElementPlusResolver} from 'unplugin-vue-components/resolvers' importComponentsfrom 'unplugin-vue-components/vite' import {defineConfig} from 'vite' export defaultdefineConfig({ssr: {noExternal: ['element-plus', /^element-plus\/.*/], },plugins: [AutoImport({imports: ['vue', 'vue-router', '@vueuse/core'],resolvers: [ElementPlusResolver(), ], }),Components({resolvers: [ElementPlusResolver(),IconsResolver({prefix: 'icon',enabledCollections: ['ep'],customCollections: ['esri'], }), ],include: [/\.vue($|\?)/, /\.md($|\?)/],extensions: ['vue', 'md'], }),Icons({customCollections: {esri:FileSystemIconLoader(resolve('./esri_symbol_3d'),svg=>svg.replace(/^<svg /, '<svg fill="currentColor" ')), }, }), ], })使用