vue-cli中使用Element(vue2.0桌面端组件库)

Element官网链接:http://element-cn.eleme.io/#/zh-CN

1、在项目中使用npm安装

npm install element-ui -save

2.1、在项目中完整引入(引入所有element组件)

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

import Vue from 'vue';
import ElementUI from 'element-ui';             //引入element-ui组件
import 'element-ui/lib/theme-chalk/index.css';  //element-ui组件的样式需要单独引入
import App from './App.vue';

Vue.use(ElementUI);  //注册element组件

new Vue({
  el: '#app',
  render: h => h(App)
});
2.2、使用

全局引入之后即可在vue的任意组件中使用element组件了。

  <el-button>默认按钮</el-button>
  <el-button type="primary">主要按钮</el-button>
  <el-button type="success">成功按钮</el-button>
  <el-button type="info">信息按钮</el-button>
  <el-button type="warning">警告按钮</el-button>
  <el-button type="danger">危险按钮</el-button>
3.1、按需引入(引入所需的 element组件)

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

1、首先,安装 babel-plugin-component:

npm install babel-plugin-component -D

2、然后将项目根目录下的 .babelrc 文件的plugins字段修改为:
{
    "plugins": [
    "transform-vue-jsx", 
    "transform-runtime",
    //添加下面这一个数组
    [
    "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}
3、在main.js中按需引入

接下来,如果你只希望引入部分组件,比如 Button 和 Select,那么需要在 main.js 中写入以下内容:(多个组件之间用英文逗号分隔)

import Vue from 'vue';
import { Button, Select } from 'element-ui';  //引入所需的组件,但是不需要再单独引入样式了
import App from './App.vue';

Vue.use(Button) //注册引入的组件
Vue.use(Select)

/* 或写为
 * Vue.component(Button.name, Button);
 * Vue.component(Select.name, Select);
 */

new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
3.2、使用

然后可以在vue中的任意组件使用已经引入的element组件了,如果没有引入的组件就不可以使用,否则就会报错

  <el-button>默认按钮</el-button>
  <el-button type="primary">主要按钮</el-button>
  <el-button type="success">成功按钮</el-button>
  <el-button type="info">信息按钮</el-button>
  <el-button type="warning">警告按钮</el-button>
  <el-button type="danger">危险按钮</el-button>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值