yarn elementui
安装 Element UI 到 Yarn 项目
确保项目已初始化并包含 package.json 文件。若未初始化,运行 yarn init 创建项目。
通过 Yarn 添加 Element UI 到项目依赖:
yarn add element-ui
引入 Element UI
在 Vue 项目中完整引入 Element UI。修改 main.js 或入口文件:
import Vue from 'vue';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);
按需引入组件(推荐)
使用 babel-plugin-component 优化体积。安装插件:
yarn add babel-plugin-component -D
配置 babel.config.js:
module.exports = {
plugins: [
[
"component",
{
"libraryName": "element-ui",
"styleLibraryName": "theme-chalk"
}
]
]
};
按需引入示例(在组件文件中):
import { Button, Select } from 'element-ui';
export default {
components: {
'el-button': Button,
'el-select': Select
}
};
全局配置
在引入 Element UI 时可设置全局属性,如默认尺寸:
Vue.use(ElementUI, { size: 'small' });
主题定制
通过覆盖 SCSS 变量自定义主题。安装主题生成工具:
yarn add element-theme -g
yarn add element-theme-chalk -D
生成主题配置文件并编译:
et -i
et
使用组件
在 Vue 模板中直接使用 Element UI 组件:
<template>
<el-button type="primary">按钮</el-button>
<el-date-picker v-model="date"></el-date-picker>
</template>
注意事项
- 确保项目依赖 Vue 2.x(Element UI 不兼容 Vue 3)。
- 按需引入需配合 Babel 插件,避免打包未使用的组件。
- 主题定制需安装 Node-sass 等预处理工具。







