vue 怎样亲手撸一个loading全局组件

1、首先建立一个 loading.vue 文件

<template lang="html">
    <div class="">
        <div class="ajax-loading load-status" v-show="showLoading">
            <div class="head-space"></div>
            <div class="loading-box">
                <div class="la-square-jelly-box">
                    <div></div>
                    <div></div>
                </div>
            </div>
        </div>
        <div class="ajax-toast load-status" v-show="showToast">
            <div class="toast">
                <div class="la-line-spin-clockwise-fade">
                    <div></div>
                    <div></div>
                    <div></div>
                    <div></div>
                    <div></div>
                    <div></div>
                    <div></div>
                    <div></div>
                </div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
  data() {
      return {

      }
  },
  props: {
    showLoading: false,
    showToast: false
  }
}
</script>
<style lang="css">
.loading-box{
    background: #f5f5f5;
    flex:1;
    -webkit-flex:1;
    height:100%;
}
.ajax-loading {
    display: flex;
    -webkit-display: flex;
    flex-direction:column;
    -webkit-flex-direction:column;
    width: 100%;
    height: 100%;
    background-color: rgba(255,255,255,0);
    position: fixed;
    z-index:10;
}
.head-space {
    height: 4.4rem;
}
.loading-box{
    color: #f5514d !important;
}
.ajax-loading .la-square-jelly-box {
    margin: -16px auto 0;
    position: relative;
    top: 50%;
}
.la-square-jelly-box,.la-square-jelly-box>div {
    position: relative;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box
}

.la-square-jelly-box {
    display: block;
    font-size: 0;
    color: #fff
}

.la-square-jelly-box.la-dark {
    color: #333
}

.la-square-jelly-box>div {
    display: inline-block;
    float: none;
    background-color: currentColor;
    border: 0 solid currentColor
}

.la-square-jelly-box {
    width: 32px;
    height: 32px
}

.la-square-jelly-box>div:nth-child(1),.la-square-jelly-box>div:nth-child(2) {
    position: absolute;
    left: 0;
    width: 100%
}

.la-square-jelly-box>div:nth-child(1) {
    top: -25%;
    z-index: 1;
    height: 100%;
    border-radius: 10%;
    -webkit-animation: square-jelly-box-animate 0.6s -0.1s linear infinite;
    -moz-animation: square-jelly-box-animate 0.6s -0.1s linear infinite;
    -o-animation: square-jelly-box-animate 0.6s -0.1s linear infinite;
    animation: square-jelly-box-animate 0.6s -0.1s linear infinite
}

.la-square-jelly-box>div:nth-child(2) {
    bottom: -9%;
    height: 10%;
    background: #000;
    border-radius: 50%;
    opacity: .2;
    -webkit-animation: square-jelly-box-shadow 0.6s -0.1s linear infinite;
    -moz-animation: square-jelly-box-shadow 0.6s -0.1s linear infinite;
    -o-animation: square-jelly-box-shadow 0.6s -0.1s linear infinite;
    animation: square-jelly-box-shadow 0.6s -0.1s linear infinite
}

.la-square-jelly-box.la-sm {
    width: 16px;
    height: 16px
}

.la-square-jelly-box.la-2x {
    width: 64px;
    height: 64px
}

.la-square-jelly-box.la-3x {
    width: 96px;
    height: 96px
}

@-webkit-keyframes square-jelly-box-animate {
    17% {
        border-bottom-right-radius: 10%
    }

    25% {
        -webkit-transform: translateY(25%) rotate(22.5deg);
        transform: translateY(25%) rotate(22.5deg)
    }

    50% {
        border-bottom-right-radius: 100%;
        -webkit-transform: translateY(50%) scale(1, 0.9) rotate(45deg);
        transform: translateY(50%) scale(1, 0.9) rotate(45deg)
    }

    75% {
        -webkit-transform: translateY(25%) rotate(67.5deg);
        transform: translateY(25%) rotate(67.5deg)
    }

    100% {
        -webkit-transform: translateY(0) rotate(90deg);
        transform: translateY(0) rotate(90deg)
    }
}

@keyframes square-jelly-box-animate {
    17% {
        border-bottom-right-radius: 10%
    }

    25% {
        -webkit-transform: translateY(25%) rotate(22.5deg);
        -moz-transform: translateY(25%) rotate(22.5deg);
        -o-transform: translateY(25%) rotate(22.5deg);
        transform: translateY(25%) rotate(22.5deg)
    }

    50% {
        border-bottom-right-radius: 100%;
        -webkit-transform: translateY(50%) scale(1, 0.9) rotate(45deg);
        -moz-transform: translateY(50%) scale(1, 0.9) rotate(45deg);
        -o-transform: translateY(50%) scale(1, 0.9) rotate(45deg);
        transform: translateY(50%) scale(1, 0.9) rotate(45deg)
    }

    75% {
        -webkit-transform: translateY(25%) rotate(67.5deg);
        -moz-transform: translateY(25%) rotate(67.5deg);
        -o-transform: translateY(25%) rotate(67.5deg);
        transform: translateY(25%) rotate(67.5deg)
    }

    100% {
        -webkit-transform: translateY(0) rotate(90deg);
        -moz-transform: translateY(0) rotate(90deg);
        -o-transform: translateY(0) rotate(90deg);
        transform: translateY(0) rotate(90deg)
    }
}

@-webkit-keyframes square-jelly-box-shadow {
    50% {
        -webkit-transform: scale(1.25, 1);
        transform: scale(1.25, 1)
    }
}

@keyframes square-jelly-box-shadow {
    50% {
        -webkit-transform: scale(1.25, 1);
        -moz-transform: scale(1.25, 1);
        -o-transform: scale(1.25, 1);
        transform: scale(1.25, 1)
    }
}

/*!
 * Load Awesome
 */
.ajax-toast {
    width: 100%;
    height: 100%;
    background-color: rgba(255,255,255,0);
    position: fixed;
    z-index: 9;
}
 .ajax-toast .toast {
    height: 7rem;
    width: 7rem;
    background-color: rgba(0,0,0,0.5);
    top: 50%;
    left:50%;
    margin-top: -3.5rem;
    margin-left: -3.5rem;
    padding-top: 1.8rem;
    position: relative;
    border-radius: 0.2rem;
 }
.ajax-toast .la-line-spin-clockwise-fade {
    margin: 0 auto;
    position: relative;
    text-align: center;
    height:3.5rem;
    width:3.5rem;
}
.la-line-spin-clockwise-fade,
.la-line-spin-clockwise-fade > div {
    position: relative;
    -webkit-box-sizing: border-box;
       -moz-box-sizing: border-box;
            box-sizing: border-box;
}
.la-line-spin-clockwise-fade {
    display: block;
    font-size: 0;
    color: #fff;
}
.la-line-spin-clockwise-fade.la-dark {
    color: #333;
}
.la-line-spin-clockwise-fade > div {
    display: inline-block;
    float: none;
    background-color: currentColor;
    border: 0 solid currentColor;
}
.la-line-spin-clockwise-fade {
    width: 32px;
    height: 32px;
}
.la-line-spin-clockwise-fade > div {
    position: absolute;
    width: 3px;
    height: 10px;
    margin: 2px;
    margin-top: -5px;
    margin-left: -1px;
    border-radius: 0;
    -webkit-animation: line-spin-clockwise-fade 1s infinite ease-in-out;
       -moz-animation: line-spin-clockwise-fade 1s infinite ease-in-out;
         -o-animation: line-spin-clockwise-fade 1s infinite ease-in-out;
            animation: line-spin-clockwise-fade 1s infinite ease-in-out;
}
.la-line-spin-clockwise-fade > div:nth-child(1) {
    top: 15%;
    left: 50%;
    -webkit-transform: rotate(0deg);
       -moz-transform: rotate(0deg);
        -ms-transform: rotate(0deg);
         -o-transform: rotate(0deg);
            transform: rotate(0deg);
    -webkit-animation-delay: -.875s;
       -moz-animation-delay: -.875s;
         -o-animation-delay: -.875s;
            animation-delay: -.875s;
}
.la-line-spin-clockwise-fade > div:nth-child(2) {
    top: 25.2512626585%;
    left: 74.7487373415%;
    -webkit-transform: rotate(45deg);
       -moz-transform: rotate(45deg);
        -ms-transform: rotate(45deg);
         -o-transform: rotate(45deg);
            transform: rotate(45deg);
    -webkit-animation-delay: -.75s;
       -moz-animation-delay: -.75s;
         -o-animation-delay: -.75s;
            animation-delay: -.75s;
}
.la-line-spin-clockwise-fade > div:nth-child(3) {
    top: 50%;
    left: 85%;
    -webkit-transform: rotate(90deg);
       -moz-transform: rotate(90deg);
        -ms-transform: rotate(90deg);
         -o-transform: rotate(90deg);
            transform: rotate(90deg);
    -webkit-animation-delay: -.625s;
       -moz-animation-delay: -.625s;
         -o-animation-delay: -.625s;
            animation-delay: -.625s;
}
.la-line-spin-clockwise-fade > div:nth-child(4) {
    top: 74.7487373415%;
    left: 74.7487373415%;
    -webkit-transform: rotate(135deg);
       -moz-transform: rotate(135deg);
        -ms-transform: rotate(135deg);
         -o-transform: rotate(135deg);
            transform: rotate(135deg);
    -webkit-animation-delay: -.5s;
       -moz-animation-delay: -.5s;
         -o-animation-delay: -.5s;
            animation-delay: -.5s;
}
.la-line-spin-clockwise-fade > div:nth-child(5) {
    top: 84.9999999974%;
    left: 50.0000000004%;
    -webkit-transform: rotate(180deg);
       -moz-transform: rotate(180deg);
        -ms-transform: rotate(180deg);
         -o-transform: rotate(180deg);
            transform: rotate(180deg);
    -webkit-animation-delay: -.375s;
       -moz-animation-delay: -.375s;
         -o-animation-delay: -.375s;
            animation-delay: -.375s;
}
.la-line-spin-clockwise-fade > div:nth-child(6) {
    top: 74.7487369862%;
    left: 25.2512627193%;
    -webkit-transform: rotate(225deg);
       -moz-transform: rotate(225deg);
        -ms-transform: rotate(225deg);
         -o-transform: rotate(225deg);
            transform: rotate(225deg);
    -webkit-animation-delay: -.25s;
       -moz-animation-delay: -.25s;
         -o-animation-delay: -.25s;
            animation-delay: -.25s;
}
.la-line-spin-clockwise-fade > div:nth-child(7) {
    top: 49.9999806189%;
    left: 15.0000039834%;
    -webkit-transform: rotate(270deg);
       -moz-transform: rotate(270deg);
        -ms-transform: rotate(270deg);
         -o-transform: rotate(270deg);
            transform: rotate(270deg);
    -webkit-animation-delay: -.125s;
       -moz-animation-delay: -.125s;
         -o-animation-delay: -.125s;
            animation-delay: -.125s;
}
.la-line-spin-clockwise-fade > div:nth-child(8) {
    top: 25.2506949798%;
    left: 25.2513989292%;
    -webkit-transform: rotate(315deg);
       -moz-transform: rotate(315deg);
        -ms-transform: rotate(315deg);
         -o-transform: rotate(315deg);
            transform: rotate(315deg);
    -webkit-animation-delay: 0s;
       -moz-animation-delay: 0s;
         -o-animation-delay: 0s;
            animation-delay: 0s;
}
.la-line-spin-clockwise-fade.la-sm {
    width: 16px;
    height: 16px;
}
.la-line-spin-clockwise-fade.la-sm > div {
    width: 1px;
    height: 4px;
    margin-top: -2px;
    margin-left: 0;
}
.la-line-spin-clockwise-fade.la-2x {
    width: 64px;
    height: 64px;
}
.la-line-spin-clockwise-fade.la-2x > div {
    width: 4px;
    height: 20px;
    margin-top: -10px;
    margin-left: -2px;
}
.la-line-spin-clockwise-fade.la-3x {
    width: 96px;
    height: 96px;
}
.la-line-spin-clockwise-fade.la-3x > div {
    width: 6px;
    height: 30px;
    margin-top: -15px;
    margin-left: -3px;
}
/*
 * Animation
 */
@-webkit-keyframes line-spin-clockwise-fade {
    50% {
        opacity: .2;
    }
    100% {
        opacity: 1;
    }
}
@keyframes line-spin-clockwise-fade {
    50% {
        opacity: .2;
    }
    100% {
        opacity: 1;
    }
}
</style>

2、主要逻辑在index.js里完成

let loadTemp = require('./loading.vue ').default;     // cli3.0以后引入vue模板需要default

let Load = {};  // 定义插件对象

Load.install = (Vue, options) => { //Vue的install方法,用于定义vue插件
    // 如果存在loading 不重复创建DOM
    if (document.getElementsByClassName('.load-status').length) return

    // 创建一个VUE构造器
    let lTemp = Vue.extend(loadTemp);

    // 提供一个在页面上已存在的DOM元素作为Vue实例的挂载目标。
    // 在实例挂载之后,可以通过$vm.$el访问。
    // 如果这个选项在实例化时有用到,实例将立即进入编译过程。否则,需要显示调用vm.$mount()手动开启编译(如下)
    // 提供的元素只能作为挂载点。所有的挂载元素会被vue生成的dom替换。因此不能挂载在顶级元素(html, body)上
    // let $vm = new toastTpl({
    //  el: document.createElement('div')
    // })

    // 实例化VUE实例
    let $vm = new lTemp();

    // 此处使用$mount来手动开启编译。用$el来访问元素,并插入到body中
    let tpl = $vm.$mount().$el;
    document.body.appendChild(tpl);

    Vue.prototype.$loading = { //在Vue的原型上添加实例方法,以全局调用
        show(options) {
            //通过传入props改变$vm下的属性控制组件 例如$vm.text = options
            if (options == "loading") {
                $vm.showLoading = true;
            } else if (options == "toast") {
                $vm.showToast = true;
            }
        },
        hide() {
            $vm.showLoading = false;
            $vm.showToast = false;
        }
    }
}
//导出Load
export default Load;

3、全局注册组件

//在你的main.js里通过以下方式引入组件

import Loading from '@/components/loading'

//然后通过 USE方法全局注册

Vue.use(Loading);

4、全局调用

//最后恭喜你,现在可以全局调用loading组件啦!

this.$loading.show(options) //注意这里的this指向的是全局对象VUE
this.$loading.hide(options)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,302评论 5 470
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,232评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,337评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,977评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,920评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,194评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,638评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,319评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,455评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,379评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,426评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,106评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,696评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,786评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,996评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,467评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,043评论 2 341

推荐阅读更多精彩内容

  • vue概述 在官方文档中,有一句话对Vue的定位说的很明确:Vue.js 的核心是一个允许采用简洁的模板语法来声明...
    li4065阅读 7,184评论 0 25
  • 我作为一个万年单身狗被老友唾弃得很,早几年面对追求者说的是弱水三千我只取一瓢饮,单了几年后变成了弱水三千 不渴 谢...
    从南以涯阅读 229评论 0 0
  • 序 由于Andoird的UI元素采用的单一线程模型,只能在UI线程进行更新,于是我们用来更新UI,常用Handle...
    andChen阅读 524评论 0 3
  • 一个人的一生面临着很多纷繁复杂的十字路口,每个十字路口前面,你都必须要做出选择,有些选择是主动的,而有些选择却是逼...
    霞姝儿阅读 392评论 0 1
  • 离考试的日子越来越近啦,索性找的工作不是很忙,老板也通融,木有事情的时候可以好好的看看书本。几年前背得滚瓜烂熟的句...
    疏碧湖的砍柴人阅读 277评论 0 1