//1.需要安装unplugin-vue-components 和 unplugin-auto-import这两款插件,以及element-plus
npm install -D unplugin-vue-components unplugin-auto-import
npm install element-plus --save
//2.安装完成之后在vue.config.js文件中配置
// vue.config.js
const AutoImport = require('unplugin-auto-import/webpack')
const Components = require('unplugin-vue-components/webpack')
const { ElementPlusResolver } = require('unplugin-vue-components/resolvers')
module.exports = {
outputDir: './build',
// 和webpapck属性完全一致,最后会进行合并
configureWebpack: {
resolve: {
alias: {
components: '@/components'
}
},
//配置webpack自动按需引入element-plus,
plugins: [
AutoImport({
resolvers: [ElementPlusResolver()]
}),
Components({
resolvers: [ElementPlusResolver()]
})
]
}
}
//3.注意:el-icon还是需要在main.js手动导入
import * as ElIcon from '@element-plus/icons-vue'
Object.keys(ElIcon).forEach(function (key) {
app.component(ElIcon[key].name, ElIcon[key])
})