整合elementui
整合 Element UI 的方法
Element UI 是一个基于 Vue.js 的组件库,整合到项目中的方法如下:
安装 Element UI 通过 npm 或 yarn 安装 Element UI:
npm install element-ui
# 或
yarn add element-ui
全局引入
在项目的入口文件(如 main.js)中引入 Element UI 及其样式:
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
按需引入
如果希望减小打包体积,可以使用按需引入的方式。安装 babel-plugin-component:
npm install babel-plugin-component -D
# 或
yarn add babel-plugin-component --dev
在 babel.config.js 中配置插件:

module.exports = {
plugins: [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
};
然后在需要使用的地方单独引入组件:
import { Button, Select } from 'element-ui';
Vue.component(Button.name, Button);
Vue.component(Select.name, Select);
自定义主题 如果需要修改默认主题,可以使用 Element UI 的主题工具。安装主题生成工具:
npm install element-theme -g
npm install element-theme-chalk -D
初始化变量文件并编译主题:

et -i
et
在项目中引入自定义主题文件:
import '../theme/index.css';
使用组件 在 Vue 单文件组件中直接使用 Element UI 的组件:
<template>
<el-button type="primary">按钮</el-button>
</template>
注意事项
- 确保项目已安装 Vue.js 2.x 版本,Element UI 不支持 Vue 3。
- 按需引入时,未使用的组件不会被打包,但需要正确配置 Babel 插件。
- 自定义主题时,需在
element-variables.scss中修改变量后重新编译。






