vite vue3 全局按需自动引入element plus
按需引入需要借助而外的插件实现
1自动导入
需要安装unplugin-vue-components 和 unplugin-auto-import这两款插件
npm install unplugin-vue-components unplugin-auto-import
Vite配置
// vite.config.js
import { defineConfig } from ‘vite’
import vue from ‘@vitejs/plugin-vue’
import path from ‘path’
import AutoImport from ‘unplugin-auto-import/vite’
import Components from ‘unplugin-vue-components/vite’
import { ElementPlusResolver } from ‘unplugin-vue-components/resolvers’
export default defineConfig({
resolve: {
alias:{
‘@’: path.resolve( __dirname, ‘./src’ ),
// 可以添加更多别名
},
// 在开发服务器运行时清理缓存
dedupe: [‘react’],
},
plugins: [
vue(),
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
],
})