浅学一下element-ui

element-ui第三方组件的使用
1、安装npm依赖
npm i element-ui -S
2、引入element-ui

完整引入:

//在mian.js文件夹中写入一下内容

import ElementUI from 'element-ui';
//引入element-ui的样式
import 'element-ui/lib/theme-chalk/index.css';

Vue.use(ElementUI);

按需引入:(减小项目体积的目的)

安装babel-plugin-component依赖

npm install babel-plugin-component -D

然后,修改.babelrc文件:(文件在vue项目下)

{
  "presets": [["es2015", { "modules": false }]],
  "plugins": [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

然后,部分引入组件,比如Button和Select

//在main.js下写入一下代码

import {Button,Select}from 'element-ui';

//使用组件,两种方法,使用其中一个
//第一种方法
Vue.component(Button.name,Button);
Vue.component(Select.name,Select);

//第二种方法
Vue.use(Button)
Vue.use(Select)



3、Button的使用
<el-button type="primary">主要按钮</el-button>
4、Button与Notification通知的结合使用
<el-button plain @click="open1"> 弹出就关闭 </el-button>
<el-button plain @click="open2"> 弹出不关闭 </el-button>


methods: {
      open1() {
        const h = this.$createElement;
        //element注册了$notify方法
        this.$notify({
          title: '标题名称',
          message: h('i', { style: 'color: teal'}, '这是提示文案:弹出两秒'),
          duration:2000
        });
      },

      open2() {
        this.$notify({
          title: '提示',
          message: '这是一条不会自动关闭的消息',
          //duration设置为0表示不会自动关闭,默认设置为4500ms
          duration: 0
        });
      }
    }
5、Tree树形控件的使用
<el-tree :data="data"  @node-click="handleNodeClick"></el-tree>

data() {
      return {
        data: [{
          label: '一级 1',
          children: [{
            label: '二级 1-1',
            children: [{
              label: '三级 1-1-1'
            }]
          }]
        }, {
          label: '一级 2',
          children: [{
            label: '二级 2-1',
            children: [{
              label: '三级 2-1-1'
            }]
          }, {
            label: '二级 2-2',
            children: [{
              label: '三级 2-2-1'
            }]
          }]
        }, {
          label: '一级 3',
          children: [{
            label: '二级 3-1',
            children: [{
              label: '三级 3-1-1'
            }]
          }, {
            label: '二级 3-2',
            children: [{
              label: '三级 3-2-1'
            }]
          }]
        }]
      };
    },
    methods: {
      handleNodeClick(data) {
        console.log(data);
      }
    }
6.手敲弹框

教程:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

7.手敲notice组件:

<template>
    <div v-if="isShow" class="el-notification" style="top: 16px;z-index:2000;">
      <h2 class="el-notification_title">{{ title }}</h2>
      <div class="el-notification_content">
        <p>{{ message }}</p>
      </div>
    </div>

</template>
<script>
export default {
  data() {
    return {
      title: "",
      message: "",
      duration:4500,
      isShow:false,
    };
    
  },
  methods:{
        hide(){
            this.isShow=false;
        }
    },
    //dom元素被挂载完成的时候调用mounted
    mounted(){
        console.log("dom挂载完成");
        this.isShow=true;
        setTimeout(()=>{
            this.isShow=false;
        },this.duration)
    }
};
</script>
<style scoped>
.el-notification.right {
  right: 16px;
}
.el-notification {
  right: 16px;
  display: flex;
  flex-direction:column;
  width: 330px;
  padding: 14px 26px 14px 13px;
  border-radius: 8px;
  box-sizing: border-box;
  border: 1px solid #ebeef5;
  position: fixed;
  background-color: #fff;
  transition: opacity 0.3s, transform 0.3s, left 0.3s, right 0.3s, top 0.4s,
    bottom 0.3s, -webkit-transform 0.3s;
  overflow: hidden;
}
.el-notification_title {
  font-weight: 700;
  font-size: 16px;
  color: #303133;
  margin: 0;
}
h2 {
  display: block;
  margin-block-start: 0.83em;
  margin-block-end: 0.83em;
  margin-inline-start: 0px;
  margin-inline-end: 0px;
}
.el-notification_content {
  font-size: 14px;
  line-height: 21px;
  margin: 6px 0 0;
  color: #606266;
  text-align: justify;
}
p {
    display: block;
    margin-block-start: 1em;
    margin-block-end: 1em;
    margin-inline-start: 0px;
    margin-inline-end: 0px;
}
</style>

被调用的构造器:

import Vue from 'vue';

//导入组件
import Notice from './Noitce.vue'

//创建构造器
const NoitceContructor=Vue.extend(Notice);

//弹框供外界使用,所以要export
export default function notice(opions){
    /*  
    //options变量传递过来的参数格式
        options:{
            tittle:"XXX",
            message:'000'
        }
    */
   //1.实例化构造器
   const instance=new NoitceContructor({
        data:opions
   })
    //2.手动挂载
    //$mount('#app') 手动挂载到id为app的dom中的意思
    instance.$mount();//dom元素渲染完成
    //手动挂载到body
    document.body.appendChild(instance.$el) //instance.$el获取当前dom对象

    return instance
}

main.js:

import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui';
//引入element-ui的样式
import 'element-ui/lib/theme-chalk/index.css';

import notice from './components/notice/index'

Vue.prototype.$notice=notice;

Vue.use(ElementUI);

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  render: h => h(App)
}).$mount('#app')

app.js

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <HelloWorld/>
    <el-button plain @click="open3"> 手敲弹框 </el-button>
  </div>
</template>

<script>
import HelloWorld from './components/HelloWorld'

export default {
  name: 'App',
  components: {
    HelloWorld
  },
  methods:{
    open3(){
      this.$notice({
        title:'组件化设计',
        message:'弹出内容',
        duration:2000
      })
    }
  }
}
</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;
}
</style>

8.手敲tree树形控件(递归实现)

tree.vue中写一下代码:

<template>
  <div>
    <div @click='toggle'>
      {{ treeData.label }}
      <span v-if='isOpenFolder'>[{{ open ? "-" : "+" }}]</span>
    </div>
    <ul v-if='isOpenFolder' v-show='open'>
      <Item
        v-for="data in treeData.children"
        :treeData="data"
        :key="data.lable"
      ></Item>
    </ul>
  </div>
  <!-- 递归组件,主要是用v-for遍历 -->
</template>
<script>
export default {
  name: "Item", //name对递归组件是必须的,递归调用组件自己
  props: {
    treeData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      open: false,
    };
  },
  methods:{
      toggle(){
          if(this.isOpenFolder){
              this.open=!this.open; //取向反
          }
      }
  },
  computed:{
      isOpenFolder(){
          return this.treeData.children && this.treeData.children.length;
      }
  }
  
};
</script>
<style scoped>
</style>

app.vue中写入以下代码:

<template>
  <div id="app">
    <img src="./assets/logo.png" />
    <HelloWorld />
    <el-button plain @click="open3"> 手敲弹框 </el-button>
    <el-tree :data="data"></el-tree>
    <m-tree :treeData="treeData"></m-tree>
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";
import MTree from "./components/tree/Tree.vue";

export default {
  name: "App",
  components: {
    HelloWorld,
    MTree,
  },
  data() {
    return {
      data: [
        {
          label: "一级 1",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 2",
          children: [
            {
              label: "二级 2-1",
              children: [
                {
                  label: "三级 2-1-1",
                },
              ],
            },
            {
              label: "二级 2-2",
              children: [
                {
                  label: "三级 2-2-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 3",
          children: [
            {
              label: "二级 3-1",
              children: [
                {
                  label: "三级 3-1-1",
                },
              ],
            },
            {
              label: "二级 3-2",
              children: [
                {
                  label: "三级 3-2-1",
                },
              ],
            },
          ],
        },
      ],
      treeData: {
        label: "web全栈课程",
        children: [
          {
            label: "js进阶+ES6",
            children: [
              {
                label: "js进阶+ES6",
                children: [
                  {
                    label: "js进阶+ES6",
                  },
                  {
                    label: "js进阶+ES6",
                  },
                ],
              },
            ],
          },
          {
            label: "js进阶+ES6",
            children: [
              {
                label: "js进阶+ES6",
                children: [
                  {
                    label: "js进阶+ES6",
                  },
                  {
                    label: "js进阶+ES6",
                  },
                ],
              },
            ],
          },
        ],
      },
    };
  },
  methods: {
    open3() {
      this.$notice({
        title: "组件化设计",
        message: "弹出内容",
        duration: 2000,
      });
    },
  },
};
</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;
}
</style>

9.手敲折叠面板

app.vue

<template>
  <div id="app">
    <img src="./assets/logo.png" />
    <HelloWorld />
    <el-button plain @click="open3"> 手敲弹框 </el-button>
    <!-- :data是自定义的 -->
    <el-tree :data="data"></el-tree>  
    <!-- :treeData是自定义的,重点是后面绑定的数据 -->
    <m-tree :treeData="treeData"></m-tree>

    
  </div>
</template>

<script>
import HelloWorld from "./components/HelloWorld";
import MTree from "./components/tree/Tree.vue";

export default {
  name: "App",
  components: {
    HelloWorld,
    MTree,
  },
  data() {
    return {
      data: [
        {
          label: "一级 1",
          children: [
            {
              label: "二级 1-1",
              children: [
                {
                  label: "三级 1-1-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 2",
          children: [
            {
              label: "二级 2-1",
              children: [
                {
                  label: "三级 2-1-1",
                },
              ],
            },
            {
              label: "二级 2-2",
              children: [
                {
                  label: "三级 2-2-1",
                },
              ],
            },
          ],
        },
        {
          label: "一级 3",
          children: [
            {
              label: "二级 3-1",
              children: [
                {
                  label: "三级 3-1-1",
                },
              ],
            },
            {
              label: "二级 3-2",
              children: [
                {
                  label: "三级 3-2-1",
                },
              ],
            },
          ],
        },
      ],
      treeData: {
        label: "web全栈课程",
        children: [
          {
            label: "js进阶+ES6",
            children: [
              {
                label: "js进阶+ES6",
                children: [
                  {
                    label: "js进阶+ES6",
                  },
                  {
                    label: "js进阶+ES6",
                  },
                ],
              },
            ],
          },
          {
            label: "js进阶+ES6",
            children: [
              {
                label: "js进阶+ES6",
                children: [
                  {
                    label: "js进阶+ES6",
                  },
                  {
                    label: "js进阶+ES6",
                  },
                ],
              },
            ],
          },
        ],
      },
    };
  },
  methods: {
    open3() {
      this.$notice({
        title: "组件化设计",
        message: "弹出内容",
        duration: 2000,
      });
    },
  },
};
</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;
}
</style>

Tree.vue中

<template>
  <div>
    <div @click='toggle'>
      {{ treeData.label }}
      <span v-if='isOpenFolder'>[{{ open ? "-" : "+" }}]</span>
    </div>
    <ul v-if='isOpenFolder' v-show='open'>
      <Item
        v-for="data in treeData.children"
        :treeData="data"
        :key="data.lable"
      ></Item>
    </ul>
  </div>
  <!-- 递归组件,主要是用v-for遍历 -->
</template>
<script>
export default {
  name: "Item", //name对递归组件是必须的,递归调用组件自己
  //子组件
  props: {
    treeData: {
      type: Object, //传递过来的类型是object
      required: true,//必传
    },
  },
  data() {
    return {
      open: false,
    };
  },
  methods:{
      toggle(){
          if(this.isOpenFolder){
              this.open=!this.open; //取反向
          }
      }
  },
  computed:{
      isOpenFolder(){
          return this.treeData.children && this.treeData.children.length;
      }
  }
  
};
</script>
<style scoped>
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值