vite按需加载element-plus,减少项目体积,你必须学会
作者:互联网
1.在项目中安装
$ npm install element-plus --save
$ yarn add element-plus
$ pnpm install element-plus
2.安装对应的插件
npm install -D unplugin-vue-components unplugin-auto-import
3.在vite.config.ts中引入
<!-- vite.config.ts 代码结束 -->
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'
// 上面这上行是新增的哈
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
vue(),
// 自动引入组件和自动注册 new add
AutoImport({
resolvers: [ElementPlusResolver()],
}),
Components({
resolvers: [ElementPlusResolver()],
}),
// 按需引入结束
]
})
使用按钮
<template>
<el-row class="mb-4">
<el-button plain>Plain</el-button>
<el-button type="primary" plain>Primary</el-button>
<el-button type="success" plain>Success</el-button>
<el-button type="info" plain>Info</el-button>
<el-button type="warning" plain>Warning</el-button>
<el-button type="danger" plain>Danger</el-button>
</el-row>
<el-row>
<el-button :icon="Search" circle />
<el-button type="primary" :icon="Edit" circle />
<el-button type="success" :icon="Check" circle />
<el-button type="info" :icon="Message" circle />
<el-button type="warning" :icon="Star" circle />
<el-button type="danger" :icon="Delete" circle />
</el-row>
</template>
<script lang="ts" setup>
// 字体图标需要你引入的哈
import {
Check,
Delete,
Edit,
Message,
Search,
Star,
} from '@element-plus/icons-vue'
</script>
项目中会多两个文件
components.d.ts
auto-imports.d.ts : 你在element-plus中使用的组件,在这个文件中会自动引入的。
参考的地址有
https://www.cnblogs.com/aloneer/p/15646354.html
https://blog.csdn.net/filerat/article/details/123750595
https://element-plus.gitee.io/zh-CN/guide/installation.html#使用包管理器
标签:vue,element,unplugin,plus,import,vite 来源: https://www.cnblogs.com/IwishIcould/p/16163271.html