Element-ui

饿了么出品

  1. _针对web ui框架 (包含多个常用的组件)_
  2. 不像bootstrap(不是框架) 只是一个简单的代码段

一、 安装

安装

建议使用yarn安装,速度会快几十倍

局部安装(是依赖)

  1. npm i element-ui

CDN

目前可以通过 unpkg.com/element-ui 获取到最新版本的资源,在页面上引入 js 和 css 文件即可开始使用。

  1. <!-- 引入样式 -->
  2. <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  3. <!-- 引入组件库 -->
  4. <script src="https://unpkg.com/element-ui/lib/index.js"></script>

二、引用

1. 完全引用 开发时候建议 避免上生产(替代方案:cdn)

2. 按需引用 上生产(大大减少小dist的体积)

你可以引入整个 Element,或是根据需要仅引入部分组件。我们先介绍如何引入完整的 Element。

1.完整引入

在 main.js 中写入以下内容:

  1. import Vue from 'vue';
  2. import ElementUI from 'element-ui';
  3. import 'element-ui/lib/theme-chalk/index.css';
  4. import App from './App.vue';
  5. Vue.use(ElementUI);
  6. new Vue({
  7. el: '#app',
  8. render: h => h(App)
  9. });

以上代码便完成了 Element 的引入。需要注意的是,样式文件需要单独引入。

2.按需引入

借助 babel-plugin-component,我们可以只引入需要的组件,以达到减小项目体积的目的。

首先,安装 babel-plugin-component:

  1. npm install babel-plugin-component -D

然后,将 .babelrc 修改为:

  1. {
  2. "presets": [["es2015", { "modules": false }]],
  3. "plugins": [
  4. [
  5. "component",
  6. {
  7. "libraryName": "element-ui",
  8. "styleLibraryName": "theme-chalk"
  9. }
  10. ]
  11. ]
  12. }

接下来,如果你只希望引入部分组件,比如 Button 和 Select,那么需要在 main.js 中写入以下内容:

  1. import Vue from 'vue';
  2. import { Button, Select } from 'element-ui';
  3. import App from './App.vue';
  4. Vue.component(Button.name, Button);
  5. Vue.component(Select.name, Select);
  6. /* 或写为
  7. * Vue.use(Button)
  8. * Vue.use(Select)
  9. */
  10. new Vue({
  11. el: '#app',
  12. render: h => h(App)
  13. });

3.全局配置

在引入 Element 时,可以传入一个全局配置对象。该对象目前支持 sizezIndex 字段。size 用于改变组件的默认尺寸,zIndex 设置弹框的初始 z-index(默认值:2000)。按照引入 Element 的方式,具体操作如下:

完整引入 Element:

  1. import Vue from 'vue';
  2. import Element from 'element-ui';
  3. Vue.use(Element, { size: 'small', zIndex: 3000 });

按需引入 Element:

  1. import Vue from 'vue';
  2. import { Button } from 'element-ui';
  3. Vue.prototype.$ELEMENT = { size: 'small', zIndex: 3000 };
  4. Vue.use(Button);

按照以上设置,项目中所有拥有 size 属性的组件的默认尺寸均为 ‘small’,弹框的初始 z-index 为 3000。

三、