WEB前端:vuejs全家桶(36):使用Elment UI、使用less

二、 Elment UI

1. 简介

    Element UI是饿了么团队提供的一套基于Vue2.0的组件库,可以快速搭建网站,提高开发效率
        ElementUI  PC端
        MintUI 移动端

[官网](http://element.eleme.io/)

2. 快速上手

新建项目
vue init webpack-simple element-demo
2.1 安装elment ui
cnpm install element-ui -S
2.2 在main.js中引入并使用组件
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css' //该样式文件需要单独引入
Vue.use(ElementUI);
这种方式引入了ElementUI中所有的组件
2.3 在webpack.config.js中添加loader
CSS样式和字体图标都需要由相应的loader来加载,所以需要style-loader、css-loader

默认并没有style-loader模块,所以需要单独安装
    cnpm install style-loader --save-dev

编译运行时提示:cross-env’ 不是内部或外部命令

依赖项丢失,直接再执行下即可。
cnpm install

使用UI组件

1步:在src目录新建components目录, 在components目录新建DatePicker.vue
第2步:在App.vue的<script>引入import DatePicker from './components/DatePicker'3步:注册DatePicker
第4步:在<template>放组件DatePicker

DatePicker.vue

<template>
  <div class="block">
    <span class="demonstration">默认</span>
    <el-date-picker
      v-model="value1"
      type="date"
      size="small"
      placeholder="选择日期">
    </el-date-picker>
  </div>
  
</template>

<script>
  export default {
    data() {
      return {
        pickerOptions1: {
          disabledDate(time) {
            return time.getTime() > Date.now();
          },
          shortcuts: [{
            text: '今天',
            onClick(picker) {
              picker.$emit('pick', new Date());
            }
          }, {
            text: '昨天',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24);
              picker.$emit('pick', date);
            }
          }, {
            text: '一周前',
            onClick(picker) {
              const date = new Date();
              date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', date);
            }
          }]
        },
        value1: '',
        value2: '',
      };
    }
  };
</script>

App.vue

<template>
  <div id="app">
    <h1>{{ msg }}</h1>
    <div>
      <el-button disabled>默认按钮</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>
   </div>
   <!-- 图标 -->
   <div>
      <el-button type="primary" icon="el-icon-edit"></el-button>
      <el-button type="primary" icon="el-icon-share"></el-button>
      <el-button type="primary" icon="el-icon-delete"></el-button>
      <el-button type="primary" icon="el-icon-search">搜索</el-button>
      <el-button type="primary">上传<i class="el-icon-upload el-icon--right"></i></el-button>
   </div>
   <!-- 图标 -->
   <div>
      <i class="el-icon-edit"></i>
      <i class="el-icon-share"></i>
      <i class="el-icon-delete"></i>
   </div>
   <!-- 布局 -->
   <div>
      <el-row>
        <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
        <el-col :span="8"><div class="grid-content bg-purple-light"></div></el-col>
        <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
      </el-row>
   </div>
   <DatePicker></DatePicker>
   <Upload></Upload>
  </div>
</template>

<script>
import DatePicker from './components/DatePicker'
import Upload from './components/Upload'

export default {
  name: 'app',
  data () {
    return {
      msg: 'Welcome to Your Vue.js!'
    }   
  },
  components:{
      DatePicker,
      Upload
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>

在这里插入图片描述



#### 2.4 使用组件

#### 2.5 使用less
    第1步:安装loader,需要两个:less、less-loader
        cnpm install less less-loader -D
    第2步:在webpack.config.js中添加loader 
       {
          test: /\.less$/,
          loader: 'less-loader'
      }3步:在App.vue必须要指定lang="less" */
	
	<style lang="less"> /* 必须要指定lang="less" */
  .grid{
    border:1px solid #ccc;
    font-size:20px;
    color:@color;
    .h(50px);
  }
  @color:red;
  .h(@height){
    height:@height;
  }
</style>

### 3. 按需引入组

#### 3.1 安装babel-plugin-component
    cnpm install babel-plugin-component -D  

#### 3.2 配置.babelrc文件
    "plugins": [["component", [
        {
          "libraryName": "element-ui",
          "styleLibraryName": "theme-default"
        }
    ]]]

#### 3.3  只引入需要的插件
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值